I dump a postgresql database on one machine as follows:
sudo -i -u postgres
pg_dump ire --clean --create --format=p > xfer.sql
On a second, private machine, whose database is not exposed to the network, I restore the dump with:
sudo -i -u postgres
psql -U postgres < xfer.sql
This works once, but then any attempt to overwrite the database gets this error message:
ERROR: database "ire" is being accessed by other users
DETAIL: There is 1 other session using the database.
I can easily fix it using e.g. this solution but how can I avoid creating this dangling session in the first place?