1. Home
  2. PostgreSQL
  3. Tutorials
  4. DROP DATABASE

DROP DATABASE

In PostgreSQL DROP DATABASE statement is used for removing any existing database in server. Login to PostgreSQL database command prompt using command ‘sudo -u postgres psql‘ from terminal.

Syntax :-

DROP DATABASE [IF EXISTS] database_name

Options:-

  • IF EXISTS – Using this option, drop command will not return error if database does not exists.
  • database_name – Replace with name of database.

Example :-

Login to your PostgreSQL server using command line. You will get PostgreSQL database prompt like postgres=# . Now use DROP DATABASE statement to delete database.

postgres=# DROP DATABASE mydb;

or you can use command like below

postgres=# DROP DATABASE IF EXISTS mydb;

If everything goes successfully, you will get output like “DROP DATABASE” only.

To confirm that database has been deleted use l or list command to list all databases.

postgres=# list


                            List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | en_IN   | en_IN |
 template0 | postgres | UTF8     | en_IN   | en_IN | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_IN   | en_IN | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
(3 rows)

Reference: http://www.postgresql.org/docs/9.4/static/sql-dropdatabase.html

Was this article helpful to you? Yes No
Advertisement