Update local database from heroku postgresql with minimal downtime - postgresql

I want to update my local posgres database nightly from my Heroku Postgres database.
I may have apps that are connected to the local database at the time. I don't want to have to remember to close and restart them. I don't want to keep changing my app's database name.
How can I restore the database with minimal intervention and downtime?

Put this in a bash file and call it from a cron job that'll run when you don't mind having the database change out from under you:
# copy local db from server into a file
curl -o fresh.db `heroku pgbackups:url --app myapp`
# make a new local database with a temporary name
psql -c 'CREATE DATABASE freshdb;'
# restore the fresh database from the file
pg_restore --verbose --clean --no-acl --no-owner -h localhost -d freshdb fresh.db
# kill all connections to the local postgres server so we can rename the database
# from http://stackoverflow.com/a/13023189/596939
cat <<-EOF | psql
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
where pg_stat_activity.datname = 'workingdb'
EOF
# make the fresh database the active one, drop the old one
psql -c 'ALTER DATABASE workingdb RENAME to olddb;'
psql -c 'ALTER DATABASE freshdb RENAME to workingdb;'
psql -c 'DROP DATABASE olddb;'
# clean up temporary files
rm fresh.db
Your app should be set up to automatically reconnect to the database when it notices it has lost a connection; you're all set! Next time you use your app locally it'll have an updated copy of the db.

Related

Restoring a text `pg_dump` leaves a dangling session

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?

How do you create a database from a backup with PostgreSQL?

In SQL Server, I can create a new database on the server from a backup from another box. I can't seem to find that functionality in pgAdmin4 with PostgreSQL10. It seems like all I can do is restore an existing database. Do I need to just create a blank database first?
You can use --create or -C in pg_dump function to make database. From Documentation :
Begin the output with a command to create the database itself and
reconnect to the created database. (With a script of this form, it
doesn't matter which database in the destination installation you
connect to before running the script.)
For syntax :
pg_dump -C -U your_user -d your_db > filename.sql
or
pg_dump --create -U your_user -d your_db > filename.sql

PostgreSQL Backup and Restore (Multiple Instances)

Can anyone help me out how to backup and restore a specific database instance if I have multiple PostgreSQL instances running on a single server?
For example, I have db1, db2 and db3 on a single server. How do I backup db1 and restore it without affecting db2 and db3?
Here's how I restart the instances separately.
/usr/pgsql-9.6/bin/pg_ctl restart -D /var/lib/pgsql/9.6/db1
/usr/pgsql-9.6/bin/pg_ctl restart -D /var/lib/pgsql/9.6/db2
/usr/pgsql-9.6/bin/pg_ctl restart -D /var/lib/pgsql/9.6/db3
Thank you, #FatFreddy.
I was able to backup and restore a specific database instance on a server having multiple PostgreSQL instances using the following commands:
Backup: pg_dumpall -p 5435 > /var/lib/pgsql/9.6/db1/PostgreSQL_db1_{date}.sql
Restore: psql -U postgres -p 5435 -f /var/lib/pgsql/9.6/db1/PostgreSQL_db1_{date}.sql

Restore Postgres database using pg_restore over SSH

I have a database server without much disk space, so I took a backup of the entire db (let's just call it redblue) and saved it locally using the following command (I don't have pg running on my computer):
ssh admin#w.x.y.z "pg_dump -U postgres redblue -h localhost " \
>> db_backup_redblue.sql
I'd like to now restore it to another server (1.2.3.4) which contains an older version of "redblue" database - however wanted to ask if this is right before I try it:
ssh admin#1.2.3.4 "pg_restore -U postgres -C redblue" \
<< db_backup_redblue.sql
I wasn't sure if I need to do -C with the name of the db or not?
Will the above command overwrite/restore the remote database with the file I have locally?
Thanks!
No, that will do nothing good.
You have to start pg_restore on the machine where the dump is. Actually, since this is a plain format dump, you have to use psql rather than pg_restore:
psql -h 1.2.3.4 -U postgres -d redblue -f db_backup_redblue.sql
That requires that there is already an empty database redblue on the target system.
If you want to replace an existing database, you have to use the --clean and --create options with pg_dump.
If you want to use SSL, you'll have to configure the PostgreSQL server to accept SSL connections, see the documentation.
I'd recommend the “custom” format of pg_dump.
Of course, you can do this :) Assuming you use ssh keys to authorize user from source host to destination host.
On the source host you do the pg_dump, then pipe through ssh to destination host like this:
pg_dump -C nextcloud | ssh -i .ssh/pg_nextcloud_key postgres#192.168.0.54 psql -d template1
Hope that helps ;)

Fail to restore a postgres db

i had no problem to restore a postgres database but today when i tried to restore it as before it gave me a error, here whats i have done:
i tried to restore a postgres database as follows:
mostafa#jamareh:~/.../DB$ sudo -u postgres psql -d dbname -f dbname.sql
but it gives me this:
could not change directory to "/home/....../DB"
dbname.sql: No such file or directory
also i tried sudo -su postgres... but it doesn't work neither
what can i do to resolve it ?
thanks in advance,
From an initial look, it could be that the postgres UNIX user does not have access to your dbname.sql file because of permission issues.
You could either change the permissions (or move the file to a place that is readable by UNIX user postgres, say, /tmp), or, better, restore the database from your own UNIX user, but with the proper database user. For example, you would have:
mostafa#jamareh:~/.../DB$ psql -d dbname -U postgres -f dbname.sql
Note that there is no sudo, but that you tell psql to run the script as DB user postgres (assuming that you have the credentials for the postgres DB user).