Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dropdb mydb not working in postgres

I am a complete beginner with postgresql. I created a test database called iswdp and now I want to delete it. When I do:

dropdb iswdp

the command returns no output and when I \list the table iswdp is still there.

dropdb iswdp;

returns:

ERROR: syntax error at or near "dropdb" LINE 1: dropdb iswdp

I used:

SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE datname = current_database()
AND pid <> pg_backend_pid();

which I found from another stackoverflow post to disconnect from all databases then attempted dropdb iswdp again with the same result.

I'm stuck, can someone help me? I'm doing this on linux mint from the bash terminal.

like image 801
William234234 Avatar asked Jul 18 '17 16:07

William234234


People also ask

How do you force delete a DB in Postgres?

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. Similarly, DROP DATABASE FORCE will do the same. In the first terminal, create a test database and a database test, and connect to the database.

How do I drop a current open database?

Go to edit connections and look at the database name. Switch the connection to a different database and then drop the database you wish.

How do I list databases in PostgreSQL?

Step 1: Log in to the server using the SQL Shell (psql) app. Step 2: Run the following query: SELECT datname FROM pg_database; psql runs the query against the server and displays a list of existing databases in the output.

How do I switch between databases in PostgreSQL?

Postgres has a different way to switch databases, you do so using one of its meta-commands. Once you are in the Postgres terminal, you enter using the psql command, you can switch databases with \connect <db_name> command or with the shorter \c <db_name> .


1 Answers

The command dropdb is a command issued from a shell prompt. From a sql prompt (like psql), you would want to issue a DROP DATABASE command.

I recommend opening psql and issuing DROP DATABASE iswdp;. That should work.

You may get an error that looks like ERROR: cannot drop the currently open database, which will happen if you connect to iswdp and try to drop it. If that happens, try instead to connect to the postgres database, and issue the same DROP DATABASE command. It should work.

like image 111
jmelesky Avatar answered Sep 18 '22 05:09

jmelesky