Banco de dados DROP PostgreSQL.
Using DROP DATABASE
This command drops a database. It removes the catalog entries for the database and deletes the directory containing the data. It can only be executed by the database owner. This command cannot be executed while you or anyone else is connected to the target database (connect to Postgres or any other database to issue this command).
Syntax
The syntax for DROP DATABASE is given below −
DROP DATABASE [ IF EXISTS ] <name>
Parameters
The table lists the parameters with their descriptions.
S. Não. | Parameter & Descrição |
---|---|
1 | IF EXISTSDo not throw an error if the database does not exist. A notice is issued in this case. |
2 | nameThe name of the database to remove. |
We cannot drop a database that has any open connections, including our own connection frompsql oupgAdmin III. We must switch to another database ortemplate1 if we want to delete the database we are currently connected to. Por isso, it might be more convenient to use the programdropdb instead, which is a wrapper around this command.
Exemplo
The following is a simple example, which will deletetestdb from your PostgreSQL schema −
DROP DATABASE testdb;
Using dropdb Command
PostgresSQL command line executabledropdb is a command-line wrapper around the SQL commandDROP DATABASE. There is no effective difference between dropping databases via this utility and via other methods for accessing the server. dropdb destroys an existing PostgreSQL database. The user, who executes this command must be a database super user or the owner of the database.
Syntax
The syntax fordropdb is as shown below −
dropdb [option...] <dbname>
Parameters
The following table lists the parameters with their descriptions
S. Não. | Parameter & Descrição |
---|---|
1 | dbnameThe name of a database to be deleted. |
2 | opçãocommand-line arguments, which dropdb accepts. |
Opções
The following table lists the command-line arguments dropdb accepts −
S. Não. | Option & Descrição |
---|---|
1 | -eShows the commands being sent to the server. |
2 | -euIssues a verification prompt before doing anything destructive. |
3 | -VPrint the dropdb version and exit. |
4 | –if-existsDo not throw an error if the database does not exist. A notice is issued in this case. |
5 | –ajudaShow help about dropdb command-line arguments, and exit. |
6 | -h hostSpecifies the host name of the machine on which the server is running. |
7 | -p portSpecifies the TCP port or the local UNIX domain socket file extension on which the server is listening for connections. |
8 | -U usernameUser name to connect as. |
9 | -wNever issue a password prompt. |
10 | -CForce dropdb to prompt for a password before connecting to a database. |
11 | –maintenance-db=dbnameSpecifies the name of the database to connect to in order to drop the target database. |
Exemplo
The following example demonstrates deleting a database from OS command prompt −
dropdb -h localhost -p 5432 -U postgress testdb
Password for user postgress: ****
The above command drops the databasetestdb. Here, I have used thepostgres (found under the pg_roles of template1) username to drop the database.