【发布时间】:2016-04-30 19:57:12
【问题描述】:
使用 PostgreSQL 9.2.8,我试图将我的数据从一个数据库恢复到另一个数据库,但触发器似乎仍在运行。我已经编写了如下所示的脚本来进行复制。
基本上我有dts 作为我的生产数据库,我有DigitalTrafficSystem 作为我的开发数据库。表结构是相同的,但开发者的存储过程非常不同。
当恢复运行时,DigitalTrafficSystem 数据库以一堆在dts 数据库中不存在的额外表行结束,所以我假设这些是由触发器创建的。
对于每个表,我收到的与触发器相关的消息如下所示:
pg_restore: [archiver (db)] could not execute query: ERROR: permission denied: "RI_ConstraintTrigger_c_136691" is a system trigger
Command was: ALTER TABLE usage ENABLE TRIGGER ALL;
我假设(错误地?)触发器已关闭,但系统级触发器无法被禁用。
这是我的脚本:
#!/bin/sh
PGUSER=dts
FILE=/tmp/.dts.db.$$
# Dump the schema of the DB as we want this to keep
pg_dump -s DigitalTrafficSystem -f $FILE
dropdb -U _postgres DigitalTrafficSystem
if [ $? -ne 0 ]; then
exit;
fi
createdb -U _postgres -O dts DigitalTrafficSystem
if [ $? -ne 0 ]; then
exit;
fi
# Restore the schema
psql -d DigitalTrafficSystem -f $FILE
# Dump the data of the real production database
pg_dump -Fc -a dts -f $FILE > /dev/null
if [ $? -ne 0 ]; then
exit;
fi
# Restore only the data from the real database to our development one
pg_restore -a -d DigitalTrafficSystem --disable-triggers -S dts $FILE
rm $FILE
【问题讨论】:
-
这绝对是触发器。我运行了除最后一个 pg_restore 之外的所有内容,打开了数据库,并手动删除了所有触发器,然后运行了 pg_restore 命令,我没有出现无效数据。显然,每次我想重写数据库时,我都无法手动执行此操作。
-
看起来您正在尝试将 --data-only 还原到一个空数据库(新创建,并且不包含表)您的目标数据库中也需要表定义,您不能插入到不存在的表中。
-
不知何故,我错过了在创建数据库后运行的
psql -d DigitalTrafficSystem -f $FILE中的复制,该数据库基于转储的架构进行恢复。
标签: postgresql postgresql-9.2 pg-restore