Deleting a database in PostgreSQL via PSQL
There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql.
Today, we will look at DROP DATABASE
the Psql version of the issue command and how it works in PGADMIN.
DROP
Delete the database via PSQL
using the command
First, open Psql from the file path below.
C:/Program Files/PostgreSQL/14/bin (instead of 14, you might have a different version)
Open CMD from the search bar and run the command:
C:\Users\[Your User]>cd C:/Program Files/PostgreSQL/14/bin
Now, type the following:
C:\Program Files\PostgreSQL\14\bin>psql -U postgres
If you don't understand what's going on, we recommend you read Psql Setup and Commands first.
You will be asked to enter Postgres PASSWORD
, once completed, press Enter.
You will now be in your PostgreSQL server. To view all databases, write the following command:
\l
This command will list all of your databases currently on the server.
As a side note, make sure not to modify or delete any database you are not aware of as this could be harmful and result in loss of work. Follow the tutorials and don't venture into unknown territory.
So in this tutorial, the current database is:
Output:
Suppose we want to have DROP
postgres
a database. We can achieve this as follows.
DROP DATABASE postgres;
If user
that doesn't work, another command you can issue is to use the following command.
psql -U <user> -c "drop DATABASE [DATABASE NAME]"
Be sure to include a semicolon at the end.
This command will open DROP
your database and work efficiently. However, DATABASE
it will return an error if it is currently opened or was last accessed by multiple users.
ERROR: cannot drop the currently open DATABASE
Therefore, please close your database and save your work before deleting it.
Use DROPDB
as a shell alternative to the PSQL command
Many users issue DROPDB
commands from Psql instead of CMD (Command Prompt), which is a mistake.
You have to open CMD to run this command against DROP
the database. Let us learn how to do this.
Search Windows for environment variables. In the box below, click 环境变量
.
Click NEW
to add a new value path.
In VARIABLE PATH
, enter the and folders BIN
of , which would be:PATH
LIB
C:\Program Files\PostgreSQL\14\bin
C:\Program Files\PostgreSQL\14\lib
Press Enter and close.
In CMD, enter BIN
the folder using the following command.
cd C:\Program Files\PostgreSQL\14\bin
Issue the following command.
DROPBD [DATABASE NAME]
This command will delete the database as needed. All of the above is to ensure that your system DROPDB
does not have errors when issuing the command.
Force disconnect and use DROPDB
as a shell alternative to PSQL commands
We assume that your system does not have environment variables set yet. If you do, leave them as is or edit them to the following configuration.
If you want DROP
to use forced disconnection of the database when issuing the command, you can use the keyword in the shell CMD -f
and use it in the database FORCE
.
For the command in CMD DROP
:
DROPBD -f [DATABASE NAME]
For the database interface or the PGADMIN DROP
command, use the following command:
drop database postgres with (FORCE)
FORCE
It DROP
is essential to terminate all existing connections to the database before invoking the command.-f
Alternatively, to sever all existing connections, you can continue by issuing the command with PROCESS TERMINATION
:
select pg_terminate_backend([process id]) from pg_stat_activity where datname='database name';
Using it will terminate the current session of Postgres server running in the background. After this, you can issue DROP
command to drop the database.
DROP
Another way
to issue the command is pg_hba.conf
to restrict access to the database through a configuration file. However, this is least recommended.
It is best to restart the server and call DROP
the command to sever the connection. Also, make sure that no other users are operating on the database, as they will be lost once the database is deleted.
Use DROPDB
Shell Alternative for PSQL Command as Ubuntu Users
For users in Ubuntu, you can issue the following command.
Connection Request:
sudo -i -u postgres psql
Use \l
to view available databases.
postgres=# \l
And using DROP DATABASE [database_name]
the command, you can delete the database.
IF EXISTS
You can use the command to check if the database already exists
before deleting it , because if it does not exist, DATABASE DROP
the command will return an error.
Following are the different ways to use Postgres commands:
For example, -i
the command will ask for confirmation before deleting. Therefore, you can use different syntaxes as needed.
Also, remember that once the database is deleted, we cannot recover it. That is why we have to use the confirmation flag before issuing the command.
in conclusion
We hope that you learned today about DROP
different ways to issue commands for databases present in PostgreSQL. These can be used in any way you need.
However, in some cases it is better to check if all prerequisites are present, for example in case of CMD shell, environment variables must exist beforehand.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Terminate the PostgreSQL connection
Publish Date:2025/04/11 Views:199 Category:PostgreSQL
-
In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep
Single query to rename and change column type in PostgreSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat
Joining columns using Select in PostgreSQL
Publish Date:2025/04/11 Views:176 Category:PostgreSQL
-
MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi
Using CASE in PostgreSQL
Publish Date:2025/04/11 Views:124 Category:PostgreSQL
-
This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,
Using NOT IN with subqueries in PostgreSQL
Publish Date:2025/04/11 Views:93 Category:PostgreSQL
-
NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr
Using variables in PostgreSQL
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article will demonstrate how we can declare and assign values to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y
Connect to PostgreSQL using a password
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm
Using the database in PostgreSQL
Publish Date:2025/04/11 Views:132 Category:PostgreSQL
-
This article demonstrates connecting to a database, creating a new database, and creating a table in PostgreSQL. Available databases in PostgreSQL You can run the following command after opening the Postgres command line to view all availab
PostgreSQL replace string
Publish Date:2025/04/11 Views:91 Category:PostgreSQL
-
This article discusses how to use PostgreSQL replace() functions to replace strings. Use the PostgreSQL replace() function to replace strings PostgreSQL replace() functions have the following arguments, which are all text types: replace (st