Postgres:在重新创建/重新填充bash脚本之前清除整个数据库

时间:2010-01-13 13:25:34

标签: database backup postgresql

我正在编写一个shell脚本(将成为一个cronjob):

1:转储我的生产数据库

2:将转储导入我的开发数据库

在第1步和第2步之间,我需要清除开发数据库(删除所有表?)。如何从shell脚本中最好地完成?到目前为止,它看起来像这样:

#!/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

7 个答案:

答案 0 :(得分:156)

我只是删除数据库然后重新创建它。在UNIX或Linux系统上,应该这样做:

$ dropdb development_db_name
$ createdb developmnent_db_name

实际上,我就是这样做的。

答案 1 :(得分:83)

如果您实际上不需要以纯文本.sql脚本文件格式备份转储到磁盘上的数据库,则可以通过管道将pg_dumppg_restore直接连接在一起。< / p>

要删除并重新创建表,可以使用--clean pg_dump命令行选项发出SQL命令,以便在创建它们(命令)之前清理(删除)数据库对象。 (在重新创建它们之前,这不会丢弃整个数据库,只会丢弃每个表/序列/索引等。)

以上两个看起来像这样:

pg_dump -U username --clean | pg_restore -U username

答案 2 :(得分:12)

虽然以下行是从Windows批处理脚本中获取的,但命令应该非常相似:

psql -U username -h localhost -d postgres -c "DROP DATABASE \"$DATABASE\";"

此命令用于通过实际删除它来清除整个数据库。命令中的$DATABASE(在Windows中应为%DATABASE%)是一个Windows样式环境变量,其值为数据库名称。您需要用development_db_name替换它。

答案 3 :(得分:7)

转储:

pg_dump -Fc mydb > db.dump

要恢复:

pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d my_db db/latest.dump

答案 4 :(得分:6)

我已经使用过:

pg_restore -c -d database_name filename.dump

答案 5 :(得分:2)

对于不能仅仅使用DROP SCHEMA public CASCADE;DROP OWNED BY current_user;之类的情况,这是我编写的独立SQL脚本,它是事务安全的(即,您可以将其放在{{ 1}}和BEGIN;进行测试,或者ROLLBACK;进行实际操作)并清理“所有”数据库对象……好吧,我们应用程序使用的数据库中使用的所有对象明智地添加,即:

  • 在桌子上触发
  • 对表(FK,PK,COMMIT;CHECK)的约束
  • 指示
  • UNIQUE(正常或物化)
  • 表格
  • 序列
  • 功能/过程(尽管DataBindings BindingSource可能是pg_proc.proisagg
  • 所有非默认(即不是VIEW或DB内部)模式“我们”拥有:该脚本在以“不是数据库超级用户”身份运行时很有用;超级用户可以删除 all 模式(尽管仍然显式排除了真正重要的模式)

未删除是(有些是故意的;有些仅是因为我在数据库中没有示例):

  • public模式(例如,其中包含扩展程序提供的内容)
  • 扩展名
  • 集合函数
  • 整理和其他语言环境的东西
  • 事件触发器
  • 文本搜索内容,…(有关我可能会错过的其他内容,请参见should be honoured
  • 角色或其他安全设置
  • 复合类型
  • 吐司表
  • FDW和外国表

我还有一个版本,可以删除“除了两个表之外的所有内容以及属于它们的内容”,以防有人感兴趣。差异很小。如有需要,请与我联系。

对于要还原的转储具有与数据库模式版本(例如,使用Debian public,Flyway或Liquibase / DB-Manul)不同的数据库模式版本的情况,确实很有用您要将其还原到的数据库。

SQL

dbconfig-common

在PostgreSQL 9.6(-- Copyright © 2019 -- mirabilos <t.glaser@tarent.de> -- -- 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; $$; )上测试。错误修复和进一步的改进,欢迎您!

答案 6 :(得分:0)

如果要清除名为“ example_db”的数据库:

1)登录到另一个数据库(例如'postgres'):

psql postgres

2)删除数据库:

DROP DATABASE example_db;

3)重新创建数据库:

CREATE DATABASE example_db;