PostgreSQL:通过命令行删除 PostgreSQL 数据库 [英] PostgreSQL: Drop PostgreSQL database through command line

查看:68
本文介绍了PostgreSQL:通过命令行删除 PostgreSQL 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除我的数据库并通过命令行创建一个新数据库.

我使用 psql -U username 登录,然后执行 connect template1,然后是 DROP DATABASE databasename;.>

我收到错误

<块引用>

database databasename 正在被其他用户访问

我关闭了 Apache 并再次尝试此操作,但我仍然收到此错误.我做错了什么吗?

解决方案

您可以运行 dropdb 命令:

dropdb '数据库名称'

请注意,您必须是超级用户或数据库所有者才能删除它.

您还可以检查 pg_stat_activity 视图以查看当前针对您的数据库进行的活动类型,包括所有空闲进程.

SELECT * FROM pg_stat_activity WHERE datname='database name';

请注意,从 PostgreSQL v13 开始,您可以使用

自动断开用户连接

DROP DATABASE dbname WITH (FORCE);

dropdb -f dbname

I'm trying to drop my database and create a new one through the command line.

I log in using psql -U username and then do a connect template1, followed by a DROP DATABASE databasename;.

I get the error

database databasename is being accessed by other users

I shut down Apache and tried this again but I'm still getting this error. Am I doing something wrong?

解决方案

You can run the dropdb command from the command line:

dropdb 'database name'

Note that you have to be a superuser or the database owner to be able to drop it.

You can also check the pg_stat_activity view to see what type of activity is currently taking place against your database, including all idle processes.

SELECT * FROM pg_stat_activity WHERE datname='database name';

Note that from PostgreSQL v13 on, you can disconnect the users automatically with

DROP DATABASE dbname WITH (FORCE);

or

dropdb -f dbname

这篇关于PostgreSQL:通过命令行删除 PostgreSQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆