我想复制一个生产PostgreSQL数据库到一个开发服务器。最快最简单的方法是什么?


当前回答

接受的答案是正确的,但如果你想避免交互式输入密码,你可以使用这个:

PGPASSWORD={{export_db_password}} pg_dump --create -h {{export_db_host}} -U {{export_db_user}} {{export_db_name}} | PGPASSWORD={{import_db_password}} psql -h {{import_db_host}} -U {{import_db_user}} {{import_db_name}}

其他回答

pg_dump the_db_name > the_backup.sql

然后将备份复制到您的开发服务器,使用以下方法进行恢复:

psql the_new_dev_db < the_backup.sql

先使用pg_dump,然后再使用psql或pg_restore——这取决于您是选择- fp选项还是- fc选项来使用pg_dump。

用法示例:

ssh production
pg_dump -C -Fp -f dump.sql -U postgres some_database_name
scp dump.sql development:
rm dump.sql
ssh development
psql -U postgres -f dump.sql

以数据库名称运行此命令,如果要备份,则转储数据库。

 pg_dump -U {user-name} {source_db} -f {dumpfilename.sql}

 eg. pg_dump -U postgres mydbname -f mydbnamedump.sql

现在将这个转储文件scp到您想要复制DB的远程机器。

eg. scp mydbnamedump.sql user01@remotemachineip:~/some/folder/

在远程机器上,在~/some/文件夹中运行以下命令来恢复DB。

 psql -U {user-name} -d {desintation_db}-f {dumpfilename.sql}

 eg. psql -U postgres -d mynewdb -f mydbnamedump.sql

接受的答案是正确的,但如果你想避免交互式输入密码,你可以使用这个:

PGPASSWORD={{export_db_password}} pg_dump --create -h {{export_db_host}} -U {{export_db_user}} {{export_db_name}} | PGPASSWORD={{import_db_password}} psql -h {{import_db_host}} -U {{import_db_user}} {{import_db_name}}

转储数据库:pg_dump database_name_name > backup.sql


导入数据库:psql db_name < backup.sql