如何从命令行删除PostgreSQL中的所有表?
我不想删除数据库本身,只想删除其中的所有表和所有数据。
如何从命令行删除PostgreSQL中的所有表?
我不想删除数据库本身,只想删除其中的所有表和所有数据。
当前回答
我通过关注视图增强了jamie的bash方法,因为他只关注默认的表类型“base table”。
以下bash代码首先删除视图,然后删除所有其余视图
#!/usr/bin/env bash
PGDB="yourDB"
# By exporting user & pass your dont need to interactively type them on execution
export PGUSER="PGusername"
export PGPASSWORD="PGpassword"
VIEWS=`psql -d $PGDB -t --command "SELECT string_agg(table_name, ',') FROM information_schema.tables WHERE table_schema='public' AND table_type='VIEW'"`
BASETBLS=`psql -d $PGDB -t --command "SELECT string_agg(table_name, ',') FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'"`
echo Dropping views:${VIEWS}
psql $PGDB --command "DROP VIEW IF EXISTS ${VIEWS} CASCADE"
echo Dropping tables:${BASETBLS}
psql $PGDB --command "DROP TABLE IF EXISTS ${BASETBLS} CASCADE"
其他回答
我通过关注视图增强了jamie的bash方法,因为他只关注默认的表类型“base table”。
以下bash代码首先删除视图,然后删除所有其余视图
#!/usr/bin/env bash
PGDB="yourDB"
# By exporting user & pass your dont need to interactively type them on execution
export PGUSER="PGusername"
export PGPASSWORD="PGpassword"
VIEWS=`psql -d $PGDB -t --command "SELECT string_agg(table_name, ',') FROM information_schema.tables WHERE table_schema='public' AND table_type='VIEW'"`
BASETBLS=`psql -d $PGDB -t --command "SELECT string_agg(table_name, ',') FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'"`
echo Dropping views:${VIEWS}
psql $PGDB --command "DROP VIEW IF EXISTS ${VIEWS} CASCADE"
echo Dropping tables:${BASETBLS}
psql $PGDB --command "DROP TABLE IF EXISTS ${BASETBLS} CASCADE"
您可以编写一个查询来生成SQL脚本,如下所示:
select 'drop table "' || tablename || '" cascade;' from pg_tables;
Or:
select 'drop table if exists "' || tablename || '" cascade;' from pg_tables;
如果某些表由于前一句中的级联选项而自动删除。
此外,如注释中所述,您可能需要按架构名称筛选要删除的表:
select 'drop table if exists "' || tablename || '" cascade;'
from pg_tables
where schemaname = 'public'; -- or any other schema
然后运行它。
光荣的COPY+PASTE也将发挥作用。
以下步骤可能会有所帮助(对于linux用户):
首先,通过以下命令输入postgres命令提示符:sudo-u postgres psql通过以下命令输入数据库(我的数据库名为:maoss):\c猫现在输入删除所有表格的命令:DROP SCHEMA公共级联;CREATE SCHEMA公共;向公众授予所有计划;向公众公开所有计划;现在通过以下命令退出psql:\问
如果要删除数据(而不是删除表):
-- Truncate tables and restart sequnces
SELECT 'TRUNCATE TABLE "' || table_schema || '"."' || table_name || '" RESTART IDENTITY CASCADE;'
FROM information_schema.tables
WHERE table_catalog = '<database>' AND table_schema = '<schema>';
或者,如果您想要删除表,可以使用以下sql:
-- For tables
SELECT 'DROP TABLE "' || table_schema || '"."' || table_name || '" CASCADE;'
FROM information_schema.tables
WHERE table_catalog = '<database>' AND table_schema = '<schema>';
-- For sequences
SELECT 'DROP SEQUENCE d_a_seq "' || sequence_schema || '"."' || sequence_name || '";'
FROM information_schema.sequences
WHERE sequence_catalog = '<database>' AND sequence_schema = '<schema>';
嗯,因为我喜欢从命令行工作。。。
psql -U <user> -d <mydb> -c '\dt' | cut -d ' ' -f 4 | sed -e "s/^/drop table if exists /" | sed -e "s/$/;/"
-c'\dt'将调用list tables命令。
List of relations
Schema | Name | Type | Owner
--------+-------------------+-------+----------
public | _d_psidxddlparm | table | djuser
public | _d_psindexdefn | table | djuser
现在剪切-d“”-f 4,通过管道将其输出抓取第4个字段(当使用空格作为分隔符时),即表。
然后,sed被用于给一个drop表加前缀,并在后面加后缀;命令分隔符。
|egrep“_d_”-再将其转换为grep,这样您就可以更有选择地删除哪些表。
drop table if exists _d_psidxddlparm;
drop table if exists _d_psindexdefn;
注意:如前所述,这将为\dt命令输出的列标题和末尾的总行生成伪行。我会用grepping来避免,但你可以用head和tail。