Postgresql backup & restore 備份和恢復

更多詳情參考 https://stackoverflow.com/questions/2732474/restore-a-postgres-backup-file-using-the-command-linehtml

1. 110 down vote

create backupsql

pg_dump -i -h localhost -p 5432 -U postgres -F c -b -v -f 
"/usr/local/backup/10.70.0.61.backup" old_db

restore from backuppost

pg_restore -i -h localhost -p 5432 -U postgres -d old_db -v 
"/usr/local/backup/10.70.0.61.backup"

important to set -h localhost - optionthis

---------------------------------------------------------spa

2. 50 down vote

You might need to be logged in as postgres in order to have full privileges on databases.rest

su - postgres
psql -l                      # will list all databases on Postgres cluster

pg_dump/pg_restorepostgresql

pg_dump -U username -f backup.dump database_name -Fc

switch -F specify format of backup file:code

  • c will use custom PostgreSQL format which is compressed and results in smallest backup file size
  • d for directory where each file is one table
  • t for TAR archive (bigger than custom format)

restore backup:orm

pg_restore -d database_name -U username -C backup.dump

Parameter -C should create database before importing data. If it doesn't work you can always create database eg. with command (as user postgres or other account that has rights to create databases) createdb db_name -O ownerhtm

pg_dump/psql

In case that you didn't specify the argument -F default plain text SQL format was used (or with -F p). Then you can't use pg_restore. You can import data with psql.

backup:

pg_dump -U username -f backup.sql database_name

restore:

psql -d database_name -f backup.sql

----------------------------------------------------------------------------------

3. 31 down vote

POSTGRESQL 9.1.12

DUMP:

pg_dump -U user db_name > archive_name.sql

put the user password and press enter.

RESTORE:

psql -U user db_name < /directory/archive.sql

put the user password and press enter.

-----------------------------------------------------------------------------------------

Below is my version of pg_dump which I use to restore the database:

pg_restore -h localhost -p 5432 -U postgres -d my_new_database my_old_database.backup

or use psql:

psql -h localhost -U postgres -p 5432 my_new_database < my_old_database.backup

where -h host, -p port, -u login username, -d name of database

-------------------------------------------------------------------------------------------

4. 7 down vote

Backup and restore with GZIP

For larger size database this is very good

backup

pg_dump -U user -d mydb | gzip > mydb.pgsql.gz

resore

gunzip -c mydb.pgsql.gz | psql dbname -U user

https://www.postgresql.org/docs/9.1/static/backup-dump.html

---------------------------------------------------------------------------------------------

相關文章
相關標籤/搜索