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
Related
After moving a database from postgres 9.6 to 11.6 we are having performance issues running some queries.
Have two postgres servers running in parallel. 9.6 and 11.6. Bother are AWS RDS`s.
Used the pg_dump command to dump the database to a file (11.6 postgres tools). Connected to RDS running Postgres 9.6. (PGPASSWORD=<password> pg_dump -Fc --host=<hostname> --port=5432 --username=<username> -f /tmp/filename.dump --exclude-table-data 'table_a' --exclude-table-data 'table_b' --exclude-table-data 'table_c' --exclude-table-data 'table_d' <dbname>
Used the pg_restore command to load the database to a file (11.6 postgres tools). Connected to RDS running Postgres 11.6. (pg_restore -Fc -d $PGDATABASE -v -j2 <filename>
The database restored and all seemed ok however a small number of queries are taking a very long time to run or failing.
Is there something we missed or another way we should have done this? We have run ANALYZE but it didn't help.
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 ;)
I have a dockerized postgres 9.3.5 OLTP instance that I'm going to update to 9.5.2. Instead of shutting it down and doing a pg_dumpal to a file and then load it I would like to spin up a new docker container and pipe the database pg_dumpall -h localhost -p [port] -U postgres | psql -h localhost -U postgres -p [port]. I'm thinking I could alter the postgresql.conf to be in read only mode. This would temporarily mess with my app but at least users could still SELECT. Is there a better way to go about this? Are there any big issues with putting in read only mode while I pipe the database over?
I am using postgresql on an embedded device running yocto (customized linux). The package containing postgresql does not provide psql or pg_restore. /usr/bin provides the following tools:
pg_basebackup indicates that I am able to create backups. But how am I supposed to restore a backup within a terminal? With pg_restore or psql this would not be a problem for me.
Please note: I want to use a backup created on my windows/linux computer to create the initial database on the embedded device.
Create backup of the db using command:
pg_basebackup -h {host_ip} -D pg_backup -U {USER}
for restoring just change the data folder to pg_backup. Now start the psql server.
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.