[英]Oneline bash command to dump and create PostgreSQL db
Sometimes I need to create a database called DB_DEV
which is the exact copy (same scheme and data) as already existing DB
. 有时我需要创建一个名为DB_DEV
的数据库,该数据库与已有的DB
完全相同(与方案和数据相同)。 Id DB_DEV
already exists, I want just to drop it and recreate it. ID DB_DEV
已经存在,我只想删除它并重新创建它。
I used this command: 我使用了以下命令:
"pg_dump --clean -U user db | psql db_dev"
which seemed to work but I realised that it doesn't deletes tables which aren't present in db
. 这似乎工作,但我意识到它不会删除db
中不存在的表。
So if I run this command and then create a table "table"
in DEV_DB
and run the command again, the "table"
is still present in DEV_DB
database although in DB
isn't. 因此,如果我运行此命令,然后在DEV_DB
创建表"table"
并再次运行该命令,则DEV_DB
数据库中仍然存在"table"
,尽管在DB
则没有。
Do you know how to modify the command to make it work correctly? 您知道如何修改命令以使其正常工作吗?
You could drop and recreate the database before restoring the dump: 您可以在恢复转储之前删除并重新创建数据库:
psql postgres -c 'drop database db_dev; create database db_dev;'
pg_dump --clean -U user db | psql db_dev
Or: 要么:
dropdb db_dev
createdb db_dev
pg_dump --clean -U user db | psql db_dev
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.