Postgresql linux удаление базы

How to delete postgresql database on linux

I am trying to learn postgresql on linux using the command line interface. I have added some databases a while back, following some tutorials (which I have since forgot everything I have learned). Now I want to delete these databases. I made the assumption that I should be doing this by using psql, the command-line interface to postgresql. You can see what I have tried in the following command line output, and that none of it has succeeded.

psql (9.5.6) Type "help" for help. postgres=# \list List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | template0 | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres testdb | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | (4 rows) postgres=# dropdb template1 postgres-# \list List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | template0 | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres testdb | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | (4 rows) postgres-# DROP DATABASE template1 postgres-# \list List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | template0 | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres testdb | postgres | UTF8 | en_CA.UTF-8 | en_CA.UTF-8 | (4 rows) 

Источник

Postgresql linux удаление базы

DROP DATABASE — remove a database

Synopsis

DROP DATABASE [ IF EXISTS ] name [ [ WITH ] ( option [, . ] ) ] where option can be: FORCE

Description

DROP DATABASE drops a database. It removes the catalog entries for the database and deletes the directory containing the data. It can only be executed by the database owner. It cannot be executed while you are connected to the target database. (Connect to postgres or any other database to issue this command.) Also, if anyone else is connected to the target database, this command will fail unless you use the FORCE option described below.

Читайте также:  Samsung m4020nd драйвер линукс

DROP DATABASE cannot be undone. Use it with care!

Parameters

Do not throw an error if the database does not exist. A notice is issued in this case.

The name of the database to remove.

Attempt to terminate all existing connections to the target database. It doesn’t terminate if prepared transactions, active logical replication slots or subscriptions are present in the target database.

This will fail if the current user has no permissions to terminate other connections. Required permissions are the same as with pg_terminate_backend , described in Section 9.27.2. This will also fail if we are not able to terminate connections.

Notes

DROP DATABASE cannot be executed inside a transaction block.

This command cannot be executed while connected to the target database. Thus, it might be more convenient to use the program dropdb instead, which is a wrapper around this command.

Compatibility

There is no DROP DATABASE statement in the SQL standard.

See Also

Submit correction

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.

Copyright © 1996-2023 The PostgreSQL Global Development Group

Источник

How to drop PostgreSQL database through command line [closed]

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

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

Читайте также:  Линукс команда ls это

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.

4 Answers 4

You can run the dropdb command from the command line:

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); 

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.

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"; 

Had to change it a bit to work: select pg_terminate_backend(pid) from pg_stat_activity where datname=’YourDatabase’;

Hm. Ran this, but it just reconnected immediately, after saying, «SSL connection closed unexpectedly [. ] attempting to reset: Succeeded.» Annnd, I’m back.

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:

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.

Читайте также:  Linux проверить дисковое пространство

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

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.

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; 

Источник

Оцените статью
Adblock
detector