PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL - DROP DATABASE



The PostgreSQL DROP DATABASE statement is used to drop an existing PostgreSQL database.

Syntax

The syntax of using DROP DATABASE statement in PostgreSQL is given below:

DROP DATABASE DatabaseName;

Please note that to drop any database, appropriate privilege is required. Along with this, take extra measures while dropping a database. Deleting a database will result in loss of complete information stored in the database.

After dropping the database, the following command can be used to see the list of available databases in RDBMS.

\l

OR

SELECT datname FROM pg_database;

Example: drop a database

To drop an existing database with name testDB, the following statement can be used:

DROP DATABASE testDB;

After dropping the database, the following commands can be used to see the list of databases available in RDBMS.

\l
Result:
     Name    |  Owner   | Encoding |  Collate   |   Ctype    |      Access privileges      
-------------+----------+----------+------------+------------+-----------------------------
 SQLExample1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 SQLExample2 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 SQLExample3 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 SQLExample4 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 SQLExample5 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 SQLExample6 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 SQLExample7 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
 SQLExample8 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =Tc/postgres               +
             |          |          |            |            | postgres=CTc/postgres
(8 rows)


SELECT datname FROM pg_database;
Result:
   datname      
-------------
 SQLExample1 
 SQLExample2 
 SQLExample3 
 SQLExample4 
 SQLExample5 
 SQLExample6 
 SQLExample7 
 SQLExample8     
(8 rows)