📅  最后修改于: 2020-11-30 01:14:29             🧑  作者: Mango
Drop / delete命令用于从PostgreSQL平台永久删除所有文件条目和数据目录。因此,我们必须非常谨慎地使用此命令。
在本节中,我们将学习如何删除或删除数据库,而在PostgreSQL中则不再需要。
在PostgreSQL中,我们可以通过两种方式删除数据库:
要在pgAdmin中创建数据库,我们将遵循以下步骤:
步骤1
第2步
查看结果
在此,我们将把数据库放在SQL Shell(命令行)中。
句法
删除数据库的语法如下:
句法:
DROP DATABASE [ IF EXISTS] name;
该命令包含以下参数:
Parameters | Description |
---|---|
IF EXISTS | It is an optional parameter; where the warning is displayed in the place of an error if the database does not exist. |
name | Here, we will reference the database name that we want to drop it. |
现在,我们将在命令行中使用以下命令:
步骤1
\l
第2步
Drop database Javatpoint;
ERROR: database "Javatpoint" is being accessed by other users
Detail: There is 1 other session using the database.
要删除Javatpoint数据库,我们需要遵循以下过程:
REVOKE CONNECT ON DATABASE Javatpoint from public;
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'Javatpoint';
第三步
第四步
在PostgreSQL中, dropdb是一个命令行可执行命令,涵盖了SQL drop database命令。该命令只能由作为数据库所有者或数据库超级用户的那些最终用户运行。我们可以在dropdb语句的帮助下远程删除数据库。
句法
dropdb的语法如下:
dropdb [option...] dbname
其中的选项可能是以下内容:
Options | Description |
---|---|
-e | Here e means Echo, which is used to create and send to the server. |
-i | It is used to show the verification prompt before operating any fatal job. |
–help | It allows us to help with dropdb command-line statements. |
-h host | It defines the name of the host of the system, where the server is directly executing. |
-p port | This option defines the Unix domain socket file extension, where the server is creating the connections. |
-V | we can use -V option, to print the dropdb version. |
-U username | It is used to display the user name. |
-w | if we don’t need a password screen, we can use this option. |
maintenance db-=dbname | To connect the database for dropping the target database, we will use this option to describe the database name. |
–if exists | This option will display an error rather than a warning if the database does not exist. |
-W | This option is used to prompt for a password before dropping the database. |
让我们看一个示例,在该示例中,我们将从操作系统命令提示符下删除数据库:
dropdb -h localhost -p 5432 -U postgress javatpoint
Password for user postgress: ****
在这里,我们将使用Postgres用户名删除数据库。上面的命令删除了javatpoint数据库。