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.
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.
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.
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.
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.
Did you type a ;
after the DROP DATABASE test_db
? Did PostgreSQL print a response to your command?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With