简体   繁体   English

如何删除 Postgres 上的所有数据库?

[英]How to delete all databases on Postgres?

I take daily backs of our postgres development box using: pg_dumpall -h 127.0.0.1 -U user -w |我每天使用以下命令回顾我们的 postgres 开发箱:pg_dumpall -h 127.0.0.1 -U user -w | gzip blah.gz gzip blah.gz

Since 9.0 is now a release candidate I would like to restore this daily backup on a daily basis to a postgres9.0rc1 box for testing, however I'm not sure how to script it repeatedly.由于 9.0 现在是一个候选版本,我想每天将这个每日备份恢复到 postgres9.0rc1 盒子进行测试,但是我不确定如何重复编写脚本。 Is there some directory I can nuke to do this?是否有一些目录我可以使用核弹来执行此操作?

You can do "drop cluster" and "create cluster" which will automtically erase all databases.您可以执行“删除集群”和“创建集群”,这将自动擦除所有数据库。 Erase all data in you $PGDATA directory and reinit the cluster using:擦除 $PGDATA 目录中的所有数据并使用以下命令重新初始化集群:

initdb -D /usr/local/pgsql/data

You can use:您可以使用:

$ pg_dropcluster 9.2 main
$ pg_createcluster 9.2 main
$ pg_ctlcluster 9.2 main start
$ pg_restore -f your_dump_file

where 9.2 = cluster version and main = cluster name其中9.2 = cluster versionmain = cluster name

Granted the question is 9 years old at this point, but it's still the second google result for deleting all databases.授予这个问题在这一点上已有 9 年历史,但它仍然是删除所有数据库的第二个谷歌结果。 If you just want to go from N DBs to 0 without jacking with your config and also having rummage through the file system, this is a much better answer:如果您只想从 N DBs 转到 0 而不使用您的配置并且还通过文件系统进行翻找,这是一个更好的答案:

https://stackoverflow.com/a/24548640/3499424 https://stackoverflow.com/a/24548640/3499424

From the answer, the following script will generate N drop database commands, one for each non-template DB:从答案中,以下脚本将生成 N 个drop database命令,每个非模板 DB 一个:

select 'drop database "'||datname||'";'
from pg_database
where datistemplate=false;

From there, you can edit and run manually, or pipe further along into a script.从那里,您可以手动编辑和运行,或者进一步通过管道进入脚本。 Here's a somewhat verbose one-liner:这是一个有点冗长的单行:

echo \pset pager off \copy (select 'drop database "'||datname||'";' from pg_database where datistemplate=false) to STDOUT; | psql -U <user> -d postgres | <appropriate grep> | psql -U <user> -d postgres

Explanation:解释:

  1. This is a series of pipes这是一系列管道
  2. echo \pset pager off \copy (select 'drop database "'||datname||'";' from pg_database where datistemplate=false) to STDOUT; generates a string for psql to execute生成一个字符串供 psql 执行
    1. \pset pager off ensures you get all records instead of that (54 rows) crap \pset pager off确保您获得所有记录,而不是那些(54 行)废话
    2. \copy (select 'drop database "'||datname||'";' from pg_database where datistemplate=false) to STDOUT; executes the aforementioned query, sending the result to STDOUT.执行上述查询,将结果发送到 STDOUT。 We have to do this since we lead with \pset .我们必须这样做,因为我们以\pset为首。
  3. | psql -U <user> -d postgres | psql -U <user> -d postgres pipes said string into psql, which executes it. | psql -U <user> -d postgres将所述字符串通过管道传输到 psql 中,由 psql 执行。 Replace <user> with the user you want to use<user>替换为您要使用的用户
  4. | <appropriate grep> | <appropriate grep> is for stripping out the "Pager usage is off" line | <appropriate grep>用于剥离“Pager usage is off”行
    1. Windows users can use findstr /v "Pager" or findstr /b "drop" Windows 用户可以使用findstr /v "Pager"findstr /b "drop"
    2. *nix users can use grep 'drop' *nix 用户可以使用grep 'drop'
  5. | psql -U <user> -d postgres | psql -U <user> -d postgres pipes the resulting set of drop database commands into psql again, which executes them, thus dropping all the databases | psql -U <user> -d postgres将生成的drop database命令集再次通过管道传输到 psql 中,psql 执行它们,从而删除所有数据库
  6. WARNING: Without any additional filtering, this will also drop the postgres database.警告:如果没有任何额外的过滤,这也会删除postgres数据库。 You can strip it either in the SELECT or the grep if you don't want that to happen.如果您不希望这种情况发生,您可以在SELECT或 grep 中剥离它。

Moving the database data somewhere else can achieve the same as deleting, and you can easily move it back in case you change your mind (But be sure to be careful and back up your data in any case).将数据库数据移到其他地方可以实现与删除相同的效果,并且您可以轻松地将其移回以防您改变主意(但请务必小心并在任何情况下备份您的数据)。

1) Create a directory somewhere (eg /home/USERNAME/BACKUPDIR ). 1) 在某处创建一个目录(例如/home/USERNAME/BACKUPDIR )。

2) As a superuser go to the postgresql data directory (Fedora eg /var/lib/pgsql/ ) 2) 以超级用户身份进入 postgresql 数据目录(Fedora 例如/var/lib/pgsql/

3) Move the data to your backup folder: mv data /home/USERNAME/BACKUPDIR 3)将data移动到您的备份文件夹: mv data /home/USERNAME/BACKUPDIR

4) Then reinit a new database using eg sudo postgresql-setup --initdb --unit postgresql 4) 然后使用例如sudo postgresql-setup --initdb --unit postgresql一个新数据库

以 root 身份运行此命令:

cd /var/lib/postgresql/ && sudo -u postgres psql -c "SELECT 'dropdb '||datname||'' FROM pg_database WHERE datistemplate = false AND datallowconn = true And datname NOT IN ('postgres')" |grep ' dropdb ' |  sudo -u postgres /bin/bash ; cd

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

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