How to restore a postgres database - postgresql

I did a pg_dumpall -c -f <BACKUP_FILE>.sql on a system, I'd like to do a restore on another system and have postgres create all roles/databases/tablespaces for me, is it possible?

This is clearly outlined in the PostgreSQL docs.
https://www.postgresql.org/docs/9.5/static/backup-dump.html
24.1.1. Restoring the Dump
Text files created by pg_dump are intended to be read in by the psql program. The general command form to restore a dump is
psql dbname < infile
where infile is the file output by the pg_dump command. The database dbname will not be created by this command, so you must create it yourself from template0 before executing psql (e.g., with createdb -T template0 dbname). psql supports options similar to pg_dump for specifying the database server to connect to and the user name to use. See the psql reference page for more information. Non-text file dumps are restored using the pg_restore utility.

The issue was that the most recent PE uses postgres 9.4, I could without errors restore to a postgres server running postgres 9.5. My bad, should have checked the rpm's on the PE puppetDB server first.

Related

Why is pg_dump not dumping table contents?

I am trying to take backup of a schema from a remote Postgresql server (Version:11.5). The below command used to take the backup works:
pg_dump -CFc -h host -U user -d database -n schema -f "/path/data.backup".
Below is the verbose printed, which says the table contents are also dumped:
Besides, I am using Dbeaver tool to restore the backup into the postgresql server (Version:11.5) installed in my local machine. The restore works but the tables are empty.
Is there any other option which needs to be added, to export the data into the backup file ?

Restoring a Postgresql 10 dump to Postgres 9.3 server

We are about to upgrade from pgsql 9.3 to 10.x. Part of the requirement is to be able to switch back to 9.3 in the case of some disaster (some massive but of course, unlikely incompatibility).
I tried pg_restoring a dump taken from one of our dev v. 10.x databases to a pgsql9.3 server, and got a lot of errors.
Is there any known "roll back path" from v 10.x to v 9.3?
Actually you can use Pg_Dump will give you a full sql file including all DDL and DML statements to recreate your database in another place (or restore).
You can do statement in cmd for backup use Pg_Dump
pg_dump -U username -d database > filename.sql
For more documentation and command use you can see here Pg_Dump
And you can restore use Psql command like this
psql -U username -d database -f filename.sql
You can use the pg_dump from pg9.3 to backup the pg10 database. Then use that backup and pg_restore from pg9.3 again to restore.

Error when restoring 9.5 pgdump files on 9.6 postgres

We have been using 9.5 postgres.
And we use pgdump to get backup files from that database and then subsequently use it to restore on a 9.6 postgres.
We were unable to restore successfully. Usually the minor version upgrade does not mean backwards breaking.
I am wondering what's the issue causing us to be unable to successfully restore on a 9.6 database.
We need to do so just in case we need to restore from archived data backups.
I was facing the same error when upgrading from 9.3 to 9.6.
The restore failed every time I tried but the dump was successful.
My solution to this problem was not to use the custom format! Instead I used the plain format. So I tried plain format with file extension sql, with utf8 encoding as user postgres. And obviously don't forget to include pre-data, data and post-data because otherwise your restore won't be complete. This works perfectly.
If your dump is ok, also try a full vacuum before the dump. If the vacuum is not ok, this might be your problem.
To Take Dump
pg_dump --username=postgres --format=c --file=e:/testdbdump.sql testdb
OR
pg_dump -U username databasename >>sqlfile.sql
To Restore
pg_restore --username=postgres --dbname=testdb<d:\sqlfile.sql
pg_restore --username=postgres --dbname=testdb <d:\sqlfile.sql
pg_restore --username=postgres --dbname=testdb <d:\testDBApr18.sql
pg_restore --username=postgres --dbname=testdb <c:\Backup\testdbDBApr18.sql
// PG DUMP & RESTORE THAT WORKS FOR ME
pg_dump -U postgres -h localhost --format=c testdb > db_dump_file.dump
pg_restore -U postgres -h localhost -v -d testdb db_dump_file.dump

psql:pr_staging.sql:7624: ERROR: relation "res_company" already exists

Backup command: pg_dump -U username backupdbname -f backupfilename.sql
Restore Command: psql -v ON_ERROR_STOP=1 -f backupfilename.sql -d newdbname;
Actually Tried this command. Backup is working. But while restoring it will throw error psql:pr_staging.sql:7624: ERROR: relation "res_company" already exists. Because for restore, we need one newdb. So that i am creating newdb from browser manually. Thats why i facing the error.
I am creating a new db using terminal command. But it not showing in browser localhost:8069/web/database/selector.
How to restore the backup db?
If you create a db by using Odoo's db manager (interface) there already will be basic tables (the module base will be installed automatically).
There are some ways to restore the db. For example (template0 is a default template db from postgres):
createdb -T template0 newdbname
cat backupfilename | psql newdbname
You shouldn't have a Odoo server running while doing this.
You could also use Odoo's database interface to backup and restore/duplicate databases.

Is there a way to repopulate with command-line a postgres db that I can't drop?

I'm looking to load a database from a backup.gz. The backup is raw sql generated from pg_dump -U postgres app_development -f backup.gz -Z9.
I've tried dropping the db with psql -Upostgres -c "drop database app_development" but I get:
ERROR: database "app_development" is being accessed by other users
DETAIL: There are 3 other sessions using the database.
The same thing happens when I use dropdb.
I don't want to dump to a non-ascii version so I don't think I can use pg_restore.
Also, I'm not sure if it helps, but all this is happening in docker.