postgresql 在 postgres 中删除数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13419214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
drop db in postgres
提问by dinesh707
I try dropdb mydbname
in shell. It do not give any error. But still when I call \l
it is still there.
我dropdb mydbname
在shell中尝试。它不会给出任何错误。但是当我调用\l
它时它仍然存在。
I logged into the postgres server using sudo -u postgres psql
.
我使用sudo -u postgres psql
.
Other than my main concern I need to know how to go into the database other than just staying outside of it. (as a example if I want to list the tables)
除了我主要关心的问题之外,我还需要知道如何进入数据库而不仅仅是留在数据库之外。(例如,如果我想列出表格)
回答by mys
In order to drop database you can use SQL command (but I do not understand why dropdb didn't work) DROP DATABASE mydbname
:
为了删除数据库,您可以使用 SQL 命令(但我不明白为什么 dropdb 不起作用)DROP DATABASE mydbname
:
sudo -u postgres psql -c "DROP DATABASE mydbname"
It would be good to check if database is not used:
检查是否未使用数据库会很好:
select * from pg_stat_activity where datname = 'mydbname';
The sudo -u postgres psql
connects to postgres database. You need to specify database: sudo -u postgres psql mydbname
and then you can use metdata commands like \d, \dt, \dv, ...
在sudo -u postgres psql
连接到Postgres数据库。您需要指定数据库:sudo -u postgres psql mydbname
然后您可以使用元数据命令,如 \d、\dt、\dv、...
回答by Craig Ringer
When you say "shell" ... do you mean the psql
shell, not the unix command line shell?
当您说“shell”时……您是指psql
shell,而不是 unix 命令行 shell?
I'd say you're running into this issue:
我会说你遇到了这个问题:
Postgresql not creating db with “createdb” as superuser, yet not outputting errors
Postgresql 未使用“createdb”作为超级用户创建数据库,但未输出错误
ie you're trying to use dropdb
as a psql command, when it's a unix shell command. You're not getting an error because of this:
即dropdb
,当它是 unix shell 命令时,您正尝试用作 psql 命令。您不会因此而收到错误:
In psql, why do some commands have no effect?
You didn't terminate the command with a semicolon.
您没有用分号终止命令。
回答by Sandip Subedi
Are you missing the comma(;)? This command worked for me:
您是否缺少逗号(;)?这个命令对我有用:
drop database <database_name>;