导出和导入具有不同名称的PostgreSQL数据库?

有没有办法导出Postgresql数据库,然后用另一个名称导入它?

我正在使用Postgresql和Rails,我经常从生产中导出数据,数据库名为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脚本来修改转储.我想避免这种解决方案,但如果没有其他选择,我会接受它.有没有人写过一个脚本来改变转储的数据库名称,确保没有数据被改变?

解决方案是这样倾倒它:
pg_dump --no-owner --no-acl blah > blah.psql

并像这样导入:

psql blah_devel < blah.psql > /dev/null

我仍然收到这个警告:

WARNING:  database "blah" does not exist

但其余的似乎都有效.

相关文章

来源:http://www.postgres.cn/docs/11/ 4.1.1.&#160;标识符和关键词 SQL标识符和关键词必须以一个...
来源:http://www.postgres.cn/docs/11/ 8.1.&#160;数字类型 数字类型由2、4或8字节的整数以及4或8...
来源:http://www.postgres.cn/docs/11/ 5.1.&#160;表基础 SQL并不保证表中行的顺序。当一个表被读...
来源:http://www.postgres.cn/docs/11/ 6.4.&#160;从修改的行中返回数据 有时在修改行的操作过程中...
来源:http://www.postgres.cn/docs/11/ 13.2.1.&#160;读已提交隔离级别 读已提交是PostgreSQL中的...
来源:http://www.postgres.cn/docs/11/ 9.7.&#160;模式匹配 PostgreSQL提供了三种独立的实现模式匹...