45

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.

neurix
  • 4,126
  • 6
  • 46
  • 71

2 Answers2

124

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

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
4

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

Promise Preston
  • 24,334
  • 12
  • 145
  • 143