Postgres: clear entire database before re-creating / re-populating from bash script
我正在编写一个shell脚本(将成为一个cronjob),它将:
1:转储我的生产数据库
2:将转储导入我的开发数据库
在步骤1和2之间,我需要清除开发数据库(删除所有表?)。 如何从shell脚本中最好地完成? 到目前为止,它看起来像这样:
1 2 3 4 5 6 7 8 9 | #!/bin/bash TIME=`date '+%Y'-'%m'-'%d'` # 1. export(dump) the CURRENT production DATABASE pg_dump -U production_db_name > /backup/dir/backup-${TIME}.sql # missing step: DROP ALL TABLES FROM development DATABASE so it can be re-populated # 2. LOAD the backup INTO the development DATABASE psql -U development_db_name < backup/dir/backup-${TIME}.sql |
我只是删除数据库,然后重新创建它。在UNIX或Linux系统上,应该这样做:
1 2 | $ dropdb development_db_name $ createdb developmnent_db_name |
实际上,我就是这样做的。
如果您实际上不需要以纯文本.sql脚本文件格式备份转储到磁盘上的数据库,则可以通过管道直接连接
要删除并重新创建表,可以使用
以上两个看起来像这样:
1 | pg_dump -U username --clean | pg_restore -U username |
虽然以下行是从Windows批处理脚本中获取的,但该命令应该非常相似:
1 | psql -U username -h localhost -d postgres -c"DROP DATABASE "$DATABASE";" |
此命令用于通过实际删除它来清除整个数据库。命令中的
转储:
1 | pg_dump -Fc mydb > db.dump |
恢复:
1 | pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d my_db db/latest.dump |
我用过:
1 | pg_restore -c -d database_name filename.dump |
对于不能只是
- 表上的触发器
-
对表的约束(FK,PK,
CHECK ,UNIQUE ) - 指数
-
VIEW s(正常或物化) - 表
- 序列
-
功能/程序(
pg_proc.proisagg 可能应该受到尊重) -
所有nōn-default(即不是
public 或DB-internal)架构"我们"拥有:当作为"不是数据库超级用户"运行时,该脚本很有用;超级用户可以删除所有架构(但仍然明确排除了真正重要的架构)
没有删除(有些是故意的;有些只是因为我在我们的数据库中没有例子):
-
public 架构(例如,扩展程序中提供的内容) - 扩展
- 集合函数
- 整理和其他区域设置
- 事件触发器
- 文本搜索的东西,...(见这里我可能错过的其他东西)
- 角色或其他安全设置
- 复合类型
- 吐司桌
- FDW和外国表
我还有一个版本删除"除了两个表以及属于他们的东西之外的所有内容"以防有人感兴趣;差异很小。如有必要,请联系我。
这对于您要还原的转储具有与要将其还原到的数据库不同的数据库模式版本(例如,使用Debian
SQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | -- Copyright ? 2019 -- mirabilos <[email protected]> -- -- Provided that these terms and disclaimer and all copyright notices -- are retained or reproduced in an accompanying document, permission -- is granted to deal in this work without restriction, including un‐ -- limited rights to use, publicly perform, distribute, sell, modify, -- merge, give away, or sublicence. -- -- This work is provided"AS IS" and WITHOUT WARRANTY of any kind, to -- the utmost extent permitted by applicable law, neither express nor -- implied; without malicious intent or gross negligence. In no event -- may a licensor, author or contributor be held liable for indirect, -- direct, other damage, loss, or other issues arising in any way out -- of dealing in the work, even if advised of the possibility of such -- damage or existence of a defect, except proven that it results out -- of said person’s immediate fault when using the work as intended. -- - -- Drop everything from the PostgreSQL database. DO $$ DECLARE r RECORD; BEGIN -- triggers FOR r IN (SELECT pns.nspname, pc.relname, pt.tgname FROM pg_trigger pt, pg_class pc, pg_namespace pns WHERE pns.oid=pc.relnamespace AND pc.oid=pt.tgrelid AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND pt.tgisinternal=FALSE ) LOOP EXECUTE format('DROP TRIGGER %I ON %I.%I;', r.tgname, r.nspname, r.relname); END LOOP; -- constraints #1: foreign key FOR r IN (SELECT pns.nspname, pc.relname, pcon.conname FROM pg_constraint pcon, pg_class pc, pg_namespace pns WHERE pns.oid=pc.relnamespace AND pc.oid=pcon.conrelid AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND pcon.contype='f' ) LOOP EXECUTE format('ALTER TABLE ONLY %I.%I DROP CONSTRAINT %I;', r.nspname, r.relname, r.conname); END LOOP; -- constraints #2: the rest FOR r IN (SELECT pns.nspname, pc.relname, pcon.conname FROM pg_constraint pcon, pg_class pc, pg_namespace pns WHERE pns.oid=pc.relnamespace AND pc.oid=pcon.conrelid AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND pcon.contype<>'f' ) LOOP EXECUTE format('ALTER TABLE ONLY %I.%I DROP CONSTRAINT %I;', r.nspname, r.relname, r.conname); END LOOP; -- indicēs FOR r IN (SELECT pns.nspname, pc.relname FROM pg_class pc, pg_namespace pns WHERE pns.oid=pc.relnamespace AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND pc.relkind='i' ) LOOP EXECUTE format('DROP INDEX %I.%I;', r.nspname, r.relname); END LOOP; -- normal and materialised views FOR r IN (SELECT pns.nspname, pc.relname FROM pg_class pc, pg_namespace pns WHERE pns.oid=pc.relnamespace AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND pc.relkind IN ('v', 'm') ) LOOP EXECUTE format('DROP VIEW %I.%I;', r.nspname, r.relname); END LOOP; -- tables FOR r IN (SELECT pns.nspname, pc.relname FROM pg_class pc, pg_namespace pns WHERE pns.oid=pc.relnamespace AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND pc.relkind='r' ) LOOP EXECUTE format('DROP TABLE %I.%I;', r.nspname, r.relname); END LOOP; -- sequences FOR r IN (SELECT pns.nspname, pc.relname FROM pg_class pc, pg_namespace pns WHERE pns.oid=pc.relnamespace AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND pc.relkind='S' ) LOOP EXECUTE format('DROP SEQUENCE %I.%I;', r.nspname, r.relname); END LOOP; -- functions / procedures FOR r IN (SELECT pns.nspname, pp.proname, pp.oid FROM pg_proc pp, pg_namespace pns WHERE pns.oid=pp.pronamespace AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast') ) LOOP EXECUTE format('DROP FUNCTION %I.%I(%s);', r.nspname, r.proname, pg_get_function_identity_arguments(r.oid)); END LOOP; -- nōn-default schemata we own; assume to be run by a not-superuser FOR r IN (SELECT pns.nspname FROM pg_namespace pns, pg_roles pr WHERE pr.oid=pns.nspowner AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast', 'public') AND pr.rolname=CURRENT_USER ) LOOP EXECUTE format('DROP SCHEMA %I;', r.nspname); END LOOP; -- voilà RAISE NOTICE 'Database cleared!'; END; $$; |
在PostgreSQL 9.6(
如果要清理名为"example_db"的数据库:
1)登录到另一个数据库(例如'postgres'):
1 | psql postgres |
2)删除您的数据库:
1 | DROP DATABASE example_db; |
3)重新创建您的数据库:
1 | CREATE DATABASE example_db; |