@mose
To delete a PostgreSQL database on Linux, you can follow these steps:
- Open a terminal on your Linux machine.
- Log in as the postgres user using the command:
- Once you're logged in as the postgres user, access the PostgreSQL interactive terminal by running:
- List all the databases by running the following command within the psql terminal:
- Identify the database you want to delete from the list and note its name.
- Switch to the database that you want to delete:
Make sure to replace database_name
with the name of the database you want to delete.
- Ensure that you are not connected to the database you want to delete by running:
1
|
SELECT pg_stat_get_backend_pid(pg_backend_pid()) AS pg_backend_pid;
|
If you see a non-null result, it indicates that you are still connected to the database. In that case, you can disconnect from the database by running:
1
|
SELECT pg_terminate_backend(pg_stat_get_backend_pid(pg_backend_pid()));
|
- Now you can delete the database using the following command:
1
|
DROP DATABASE database_name;
|
Replace database_name
with the name of the database you want to delete.
- To confirm the deletion, you can list all the databases again by running l within the psql terminal.
- Exit the psql terminal by running q.
- Finally, exit the postgres user's shell session by running exit or Ctrl+D.
After following these steps, the specified PostgreSQL database should be successfully deleted from your Linux system.