Menu Close

Drop a Database in PostgreSQL

drop a database in Postgresql

In this article, you will learn everything about drop a database in PostgreSQL with the help of the example.PostgreSQL provides a DROP DATABASE command that is used to drop the existing database from the PostgreSQL server.

Introduction of PostgreSQL DROP DATABASE Statement

PostgreSQL gives a DROP DATABASE command that is used to delete the existing database from the PostgreSQL server. It can only execute by the database owner. Also, it can not be executed while you are else or anyone user.

Syntax

This is the basic syntax of the DROP DATABASE statement.

DROP DATABASE [ IF EXISTS ] name

Parameter

  • IF EXISTS:- Do not throw the error the assigned database not exist. Issue a note in this case.
  • name:- name represent the name of the database which you want to drop.

Notes:- DROP DATABASE can not be executed inside a transaction block.

First of all, we will log in to psql terminal and show a list of all the databases by typing \l.

\l

Output

The above statement shows all the existing databases.


   Name    |  Owner   | Encoding |          Collate           |           Ctype            | 
-----------+----------+----------+----------------------------+----------------------------+
 demodb    | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 test      | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 postgres  | postgres | UTF8     | English_United States.1252 | English_United States.1252 |

Example: Drop a Database in PostgreSQL

Suppose we want to drop the test database, then we will issue the following command.

DROP DATABASE test;

After successful execution of the above command, you will see the DROP DATABASE message.

Introduction of PostgreSQL dropdb Statement

PostgreSQL provides a dropdb statement that directly executes on the terminal without entering psql terminal. This statement is also used to drop the existing database.

Syntax

This is the basic syntax of the PostgreSQL dropdb statement.

dropdb [OPTION]... DBNAME
  • DBNAME:- DBNAME represent the name of the database you want to drop.
  • -e or –echo:- Show the commands being sent to the server.
  • -i or –interactive:- Prompt before deleting anything.
  • -V or –version:- Output version information.
  • –if-exists:- Do not show the error of the database does not exist.
  • -? or –help:- show the help and then exit.

Connection options

  • -h or –host=HOSTNAME:- Database server host.
  • -p or –port=POST:- Database server port.
  • -U or –username=USERNAME: Username to connect as.
  • -w or –no-password:- Never prompt to password authentication.
  • -W or –password:- Prompt for password authentication.
  • –maintenance-db=DBNAME:- Alternate maintenance database name.

Example: Drop PostgreSQL Database using dropdb

Here we are going to drop the Postgres database using dropdb command.

dropdb -h localhost -U postgres test

After executing the above command, you will not able to see your database.

Conclusion

So, In this article, we have seen a total of two ways to drop a database in PostgreSQL with the help of some valid examples. You can use any one method from them to drop the Postgres database.

I hope this article is very helpful for you, If you like this article, please share and keep visiting for further PostgreSQL tutorials.

Frequently Asked Questions


How do I drop a database in PostgreSQL?

Ans:- Ans:- 1. You have to specify the name of the database you want to delete after the DROP DATABASE statement.
You can also use dropdb statements to drop the Postgres database.

How do I list the databases in PostgreSQL?

Ans:- To show the list of the databases in PostgreSQL, you have to use the \l command.

How do I force delete a database in PostgreSQL?

Ans:- PostgreSQL introduces a new command-line command dropdb command that is used to drop the database. To delete a Postgres database forcely, you have to use the -f option with dropdb command.

Reference:- Click Here

More about PostgreSQL:- Click Here

Thanks for your valuable time 👏👏 …

Create a Database in PostgreSQL
Alter Database in PostgreSQL

Related Posts