简体   繁体   English

pg_restore --clean 不会删除和清除数据库

[英]pg_restore --clean is not dropping and clearing the database

I am having an issue with pg_restore --clean not clearing the database.我遇到了 pg_restore --clean 没有清除数据库的问题。

Or do I misunderstand what the --clean does, I am expecting it to truncate the database tables and reinitialize the indexes/primary keys.还是我误解了 --clean 的作用,我希望它截断数据库表并重新初始化索引/主键。

I am using 9.5 on rds我在 rds 上使用 9.5

This is the full command we use这是我们使用的完整命令

pg_restore --verbose --clean --no-acl --no-owner -h localhost -U superuser -d mydatabase backup.dump

Basically what is happening is this.基本上正在发生的事情是这样的。

I do a nightly backup of my production db, and restore it to an analytics db for the analyst to churn and run their reports.我每晚对我的生产数据库进行备份,并将其恢复到分析数据库,以便分析师搅动和运行他们的报告。

I found out recently that the rails application used to view the reports was complaining that the primary keys were missing from the restored analytics database.我最近发现用于查看报告的 rails 应用程序抱怨恢复的分析数据库中缺少主键。

So I started investigating the production db, the analytics db etc. Which was when I realized that multiple rows with the same primary key existed in the analytics database.所以我开始调查生产数据库、分析数据库等。那时我意识到分析数据库中存在具有相同主键的多行。

I ran a few short experiments and realized that every time the pg_restore script is run it inserts duplicate data into the tables, this leads me to think that the --clean is not dropping and restoring the data.我进行了一些简短的实验并意识到每次运行 pg_restore 脚本时它都会将重复的数据插入表中,这让我认为 --clean 没有删除和恢复数据。 Because if I were to drop the schema beforehand, I don't get duplicate data.因为如果我事先删除架构,我不会得到重复的数据。

To remove all tables from a database (but keep the database itself), you have two options.要从数据库中删除所有表(但保留数据库本身),您有两种选择。

Option 1: Drop the entire schema You will need to re-create the schema and its permissions.选项 1:删除整个架构 您将需要重新创建架构及其权限。 This is usually good enough for development machines only.这通常仅适用于开发机器。

DROP SCHEMA public CASCADE;
CREATE SCHEMA public;

GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO public;

Applications usually use the "public" schema.应用程序通常使用“公共”模式。 You may encounter other schema names when working with a (legacy) application's database.在使用(传统)应用程序的数据库时,您可能会遇到其他模式名称。

Note that for Rails applications, dropping and recreating the database itself is usually fine in development.请注意,对于 Rails 应用程序,删除和重新创建数据库本身在开发中通常很好。 You can use bin/rake db:drop db:create for that.你可以使用 bin/rake db:drop db:create 。

Option 2: Drop each table individually Prefer this for production or staging servers.选项 2:单独删除每个表 首选此用于生产或登台服务器。 Permissions may be managed by your operations team, and you do not want to be the one who messed up permissions on a shared database cluster.权限可能由您的运营团队管理,您不希望成为破坏共享数据库集群权限的人。

The following SQL code will find all table names and execute a DROP TABLE statement for each.以下 SQL 代码将查找所有表名并为每个表执行一个 DROP TABLE 语句。

DO $$ DECLARE
  r RECORD;
BEGIN
  FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = current_schema()) LOOP
    EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE'; -- DROP TABLE IF EXISTS instead DROP TABLE - thanks for the clarification Yaroslav Schekin
  END LOOP;
END $$;

Original:原来的:

https://makandracards.com/makandra/62111-how-to-drop-all-tables-in-postgresql https://makandracards.com/makandra/62111-how-to-drop-all-tables-in-postgresql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM