ChatGPT解决这个技术问题 Extra ChatGPT

PostgreSQL: Drop PostgreSQL database through command line [closed]

Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 9 years ago. Improve this question

I'm trying to drop my database and create a new one through the command line.

I log in using psql -U username and then do a \connect template1, followed by a DROP DATABASE databasename;.

I get the error

database databasename is being accessed by other users

I shut down Apache and tried this again but I'm still getting this error. Am I doing something wrong?

What happens if you just run the dropdb databasename command from the command line?
It says "ERROR: cannot drop the currently open database"
Use psql -U <user> -c "drop database protodb" (without database name)
This will restart postgres and disconnect everyone: sudo service postgresql restart Then do a: dropdb -h localhost -p 5432 -U "youruser" "testdb" Notice the "" to make sure special characters go in without a hitch.
drop database <dataabase_name>; Don't forget the comma.

R
Robin

You can run the dropdb command from the command line:

dropdb 'database name'

Note that you have to be a superuser or the database owner to be able to drop it.

You can also check the pg_stat_activity view to see what type of activity is currently taking place against your database, including all idle processes.

SELECT * FROM pg_stat_activity WHERE datname='database name';

Note that from PostgreSQL v13 on, you can disconnect the users automatically with

DROP DATABASE dbname WITH (FORCE);

or

dropdb -f dbname

I'm using dropuser command to remove also the user.
Not helpful answer as for version 9. The error about opened connections appears still.
This will restart postgres and disconnect everyone: sudo service postgresql restart Then do a: dropdb -h localhost -p 5432 -U "youruser" "testdb" Notice the "" to make sure special characters go in without a hitch.
using the postgres user: sudo -u postgres dropdb 'database name'
\l to see all the databases you have.
B
Brugolo

This worked for me:

select pg_terminate_backend(pid) from pg_stat_activity where datname='YourDatabase';

for postgresql earlier than 9.2 replace pid with procpid

DROP DATABASE "YourDatabase";

http://blog.gahooa.com/2010/11/03/how-to-force-drop-a-postgresql-database-by-killing-off-connection-processes/


Had to change it a bit to work: select pg_terminate_backend(pid) from pg_stat_activity where datname='YourDatabase';
column "procpid" does not exist for amazon RDS postgres 9.6 instance
Hm. Ran this, but it just reconnected immediately, after saying, "SSL connection closed unexpectedly [...] attempting to reset: Succeeded." Annnd, I'm back.
B
Bohemian

Try this. Note there's no database specified - it just runs "on the server"

psql -U postgres -c "drop database databasename"

If that doesn't work, I have seen a problem with postgres holding onto orphaned prepared statements. To clean them up, do this:

SELECT * FROM pg_prepared_xacts;

then for every id you see, run this:

ROLLBACK PREPARED '<id>';

Sorry I am new to databases, so this is probably a stupid question, but where do I type this out? Before logging into the database right? And I should replace databasename with the name of my database right?
There's no such things as "just on the server" for postgresql. You must connect to a database. In this case you'll be connecting to the postgres database which is pretty much there just for cases like this. And a good point on prepared transactions, but in that case you should get an error message saying that's the issue.
Sorry Bohemian, but you are the one who's wrong. Here's pg_stat_activity while running createdb from the command line: postgres=# select * from pg_stat_activity ; 11564 | postgres | 22223 | 16384 | smarlowe | CREATE DATABASE test; | f | 2011-08-19 16:18:26.918933-06 | 2011-08-19 16:18:26.918933-06 | 2011-08-19 16:18:26.916578-06 | | -1 Note that this happens while running createdb from the command line in another terminal. That first field is the db that my createdb script was connected to
Why isn't this the top, #1 answer?
note this does not work for Amazon RDS. It boots you from the psql connection and performs an automatic reset and then you log back in and you're back to where you started.
S
Scott Marlowe

When it says users are connected, what does the query "select * from pg_stat_activity;" say? Are the other users besides yourself now connected? If so, you might have to edit your pg_hba.conf file to reject connections from other users, or shut down whatever app is accessing the pg database to be able to drop it. I have this problem on occasion in production. Set pg_hba.conf to have a two lines like this:

local   all         all                               ident
host    all         all         127.0.0.1/32          reject

and tell pgsql to reload or restart (i.e. either sudo /etc/init.d/postgresql reload or pg_ctl reload) and now the only way to connect to your machine is via local sockets. I'm assuming you're on linux. If not this may need to be tweaked to something other than local / ident on that first line, to something like host ... yourusername.

Now you should be able to do:

psql postgres
drop database mydatabase;