PostgreSQL: dump and restore
if you use md5 authentication technique and want to use a specific user to get db dump, you can do
$ pg_dump -U username -p 5432 dbname > filename-to-backup-to.sql
To avoid credential and username issues while restoring, you can use --no-owner
flag
$ pg_dump --no-owner -U username -p 5432 dbname > filename-to-backup-to.sql
To restore the backup use below command
$ psql -U username -d dbname -f filename-to-backup-to.sql
You can always just use the command line utility.
Dump the cluster:
pg_dumpall -p 5432 > /path/to/my/dump_file.sql
Dump a single database:
pg_dump -p 5432 mydb > /path/to/my/mydb_dump.sql
Dump the schema only:
pg_dump -p 5432 mydb -s > /path/to/my/mydb_dump_schema.sql
More in the manual.
If you want to restore to an empty database, you might want to run before restoring:
DROP DATABASE IF EXISTS mydb;
CREATE DATABASE mydb;
The --clean
option for pg_dump
is not needed in this case.
pgAdmin3 will do the trick, it has pg_dump and pg_restore included in the installer.
Backup your database no tool needed.we can do with terminal
All commands should be run as the postgres user.
sudo su - postgres
Backup a single database
pg_dump db_name > db_backup.sql
Restore a single database
psql db_name < db_backup.sql
Backup an entire postgres database cluster
pg_dumpall > cluster_backup.sql
Restore an entire postgres database cluster
psql -f cluster_backup.sql postgres
Refer this source for more commands backup commands