1

Is there any way to write DROP DATABASE [ IF EXISTS ] name from php script? Seems that I need something similar to mysql_drop_db just for Postrge.

How my $connStr in $conn = pg_connect($connStr) must look like to have rights to do this command? What db I must be connected?

2
  • DROP DATABASE is a query so you can use pg_query($conn, 'DROP DATABASE') Commented Jan 18, 2018 at 8:09
  • Should I always set db name in pgconnect? How my $connStr in $conn = pg_connect($connStr) must look like? Should I connect to db which I want to drop? Commented Jan 18, 2018 at 8:34

1 Answer 1

1
  1. You have to be connected to a different db on same cluster as superuser role or db owner

https://www.postgresql.org/docs/current/static/sql-dropdatabase.html

It can only be executed by the database owner. Also, it cannot be executed while you or anyone else are connected to the target database. (Connect to postgres or any other database to issue this command.)

  1. Have to pg_terminate_backend(pid) other connections on the db you want to drop before you drop it:

    select pg_terminate_backend(pid) from pg_stat_activity where pid <> pg_backend_pid() and datname = 'db_to_drop';

Sign up to request clarification or add additional context in comments.

1 Comment

So I can drop one database while I connected to the ANY other... Thanks, I will try

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.