Pab*_*blo 41 postgresql database restore dump
有没有办法导出 PostgreSQL 数据库,然后用另一个名称导入它?
我在 Rails 中使用 PostgreSQL,我经常从生产中导出数据,其中数据库名为 blah_production,并在开发或暂存时将其导入,名称为 blah_development 和 blah_staging。在 MySQL 上,这是微不足道的,因为导出在任何地方都没有数据库(可能除了注释),但在 PostgreSQL 上这似乎是不可能的。不可能吗?
我目前正在以这种方式转储数据库:
pg_dump blah > blah.dump
Run Code Online (Sandbox Code Playgroud)
我没有使用 -c 或 -C 选项。该转储包含以下语句:
COMMENT ON DATABASE blah IS 'blah';
ALTER TABLE public.checks OWNER TO blah;
ALTER TABLE public.users OWNER TO blah;
Run Code Online (Sandbox Code Playgroud)
当我尝试导入时
psql blah_devel < blah.dump
Run Code Online (Sandbox Code Playgroud)
我得到
WARNING: database "blah" does not exist
ERROR: role "blah" does not exist
Run Code Online (Sandbox Code Playgroud)
也许问题真的不是数据库而是角色?
如果我以这种方式倾倒它:
pg_dump --format=c blah > blah.dump
Run Code Online (Sandbox Code Playgroud)
并尝试以这种方式导入它:
pg_restore -d blah_devel < tmp/blah.psql
Run Code Online (Sandbox Code Playgroud)
我收到这些错误:
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
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我已经看到有些人使用 sed 脚本来修改转储。我想避免这种解决方案,但如果没有其他选择,我会接受。有没有人写过一个脚本来改变转储的数据库名称以确保没有数据被改变?
Pab*_*blo 45
解决方案是像这样倾倒它:
pg_dump --no-owner --no-acl blah > blah.psql
Run Code Online (Sandbox Code Playgroud)
并像这样导入它:
psql blah_devel < blah.psql > /dev/null
Run Code Online (Sandbox Code Playgroud)
我仍然收到此警告:
WARNING: database "blah" does not exist
Run Code Online (Sandbox Code Playgroud)
但其余的似乎有效。
小智 8
现在 pg_restore 有 -d 选项,您可以设置导入数据的数据库名称。
来源:
pg_dump -v -Fc mydb.dmp mydb
目的地:
createdb -T template1 mydb2
pg_restore -v -e -d mydb2 mydb.dmp