有什么方法可以导出PostgreSQL数据库,然后用另一个名称导入它吗?
我在Rails中使用PostgreSQL,我经常从生产中导出数据,其中数据库名为blah_production,并在开发或使用名称blah_development和blah_staging进行暂存时导入它。在MySQL上,这很简单,因为导出在任何地方都没有数据库(除了注释),但在PostgreSQL上,这似乎是不可能的。这不可能吗?
我目前正以这样的方式转储数据库:
pg_dump blah > blah.dump我不使用-c或-C选项。该转储包含如下语句:
COMMENT ON DATABASE blah IS 'blah';
ALTER TABLE public.checks OWNER TO blah;
ALTER TABLE public.users OWNER TO blah;当我试图用
psql blah_devel < blah.dump我得到了
WARNING: database "blah" does not exist
ERROR: role "blah" does not exist也许问题并不是真正的数据库,而是角色?
如果我以这种方式丢弃它:
pg_dump --format=c blah > blah.dump然后尝试以这样的方式导入:
pg_restore -d blah_devel < tmp/blah.psql我知道这些错误:
pg_restore: WARNING: database "blah" does not exist
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 1513; 1259 16435 TABLE checks blah
pg_restore: [archiver (db)] could not execute query: ERROR: role "blah" does not exist
Command was: ALTER TABLE public.checks OWNER TO blah;
pg_restore: [archiver (db)] Error from TOC entry 1509; 1259 16409 TABLE users blah
pg_restore: [archiver (db)] could not execute query: ERROR: role "blah" does not exist
Command was: ALTER TABLE public.users OWNER TO blah;
pg_restore: [archiver (db)] Error from TOC entry 1508; 1259 16407 SEQUENCE users_id_seq blah
pg_restore: [archiver (db)] could not execute query: ERROR: role "blah" does not exist
Command was: ALTER TABLE public.users_id_seq OWNER TO blah;
pg_restore: [archiver (db)] Error from TOC entry 1824; 0 0 ACL public postgres
pg_restore: [archiver (db)] could not execute query: ERROR: role "postgres" does not exist
Command was: REVOKE ALL ON SCHEMA public FROM postgres;
pg_restore: [archiver (db)] could not execute query: ERROR: role "postgres" does not exist
Command was: GRANT ALL ON SCHEMA public TO postgres;
WARNING: errors ignored on restore: 11有什么想法吗?
我见过一些人使用sed脚本来修改转储。我想避免那种解决办法,但如果没有别的办法,我就接受。是否有人编写了一个脚本来更改转储的数据库名称,以确保没有任何数据被更改?
发布于 2011-01-29 20:48:05
解决办法是这样抛弃它:
pg_dump --no-owner --no-acl blah > blah.psql然后像这样进口:
psql blah_devel < blah.psql > /dev/null我仍然收到这样的警告:
WARNING: database "blah" does not exist但剩下的似乎起作用了。
发布于 2011-01-28 15:51:38
如果要创建文本转储,可以在不使用CREATE DATABASE位的情况下导出数据库(即不要指定-c和-C选项到pg_dump);这将防止Postgres尝试删除、创建和连接到数据库。
如果使用一种存档格式,可以将-d选项指定为pg_restore,以命名要还原到的数据库。
有关更多细节,请查看pg_dump和pg_restore的手册页,并且不要忘记在生产系统上安装抓猴,以防我遗漏了一些重要的细节。
发布于 2015-12-09 08:07:56
现在,pg_restore有了-d选项,您可以为导入数据设置数据库名。
源
pg_dump -v -Fc mydb.dmp mydb
createdb -T template1 mydb2
pg_restore -v -e -d mydb2 mydb.dmp
https://serverfault.com/questions/228170
复制相似问题