Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL: Drop Database but DB is still there [duplicate]

I am new to PostgreSQL and I try to get my head around it. I am familiar to db's and MySQL.

I am trying to delete database, which I created since psql seems to ignore the changes I try to push through Django.

When I execute \l I get the following response:

                                  List of databases        Name       | Owner  | Encoding |   Collate   |    Ctype    | Access privileges  ------------------+--------+----------+-------------+-------------+-------------------  postgres         | neurix | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 |   test_db          | neurix | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 |   template0        | neurix | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 | =c/neurix        +                   |        |          |             |             | neurix=CTc/neurix  template1        | neurix | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 | =c/neurix        +                   |        |          |             |             | neurix=CTc/neurix  template_postgis | neurix | UTF8     | en_AU.UTF-8 | en_AU.UTF-8 |  (5 rows) 

Now I wan to drop the database "test_db" with

DROP DATABASE test_db 

but when I execute \l afterwards, the table is still there and the overview looks like about.

like image 377
neurix Avatar asked Jan 28 '12 09:01

neurix


People also ask

How do I force delete a postgres database?

A new command-line option is added to dropdb command, and a similar SQL option “FORCE” is also added in DROP DATABASE. Using the option -f or –force with dropdb command or FORCE with DROP DATABASE to drop the database, it will terminate all existing connections with the database.

What is drop cascade in PostgreSQL?

The CASCADE option allows you to remove the table and its dependent objects. The RESTRICT option rejects the removal if there is any object depends on the table. The RESTRICT option is the default if you don't explicitly specify it in the DROP TABLE statement.

How do I reclaim disk space in PostgreSQL?

If you want to actually reclaim that space on disk, making it available to the OS, you'll need to run VACUUM FULL. Keep in mind that VACUUM can run concurrently, but VACUUM FULL requires an exclusive lock on the table. You will also want to REINDEX, since the indexes will remain bloated even after the VACUUM runs.

Does dropping a table drop the index postgres?

DROP TABLE removes tables from the database. Only the table owner, the schema owner, and superuser can drop a table. To empty a table of rows without destroying the table, use DELETE or TRUNCATE . DROP TABLE always removes any indexes, rules, triggers, and constraints that exist for the target table.


2 Answers

Did you type a ; after the DROP DATABASE test_db? Did PostgreSQL print a response to your command?

like image 143
Tom Anderson Avatar answered Sep 22 '22 19:09

Tom Anderson


I had a similar issue when working on a Rails 6 application in Ubuntu 20.04 with PostgreSQL as my database.

When I run the command:

DROP DATABASE my-db; 

The database is dropped successfully, however, the schema for the database is still left.

So when I run the command:

CREATE DATABASE my-db; 

And I check the tables in the newly created database, I realized they still contained the same tables as the previously deleted database, even though I have not run any migration.

Here's how I fixed it:

Instead of running the command:

DROP DATABASE my-db; 

run the command:

DROP DATABASE IF EXISTS my-db; 

This deletes the database and it's corresponding schema.

That's all.

I hope this helps

like image 21
Promise Preston Avatar answered Sep 23 '22 19:09

Promise Preston