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?

Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
GuessWho
  • 664
  • 1
  • 6
  • 19
  • `DROP DATABASE` is a query so you can use `pg_query($conn, 'DROP DATABASE')` – Hamza Abdaoui Jan 18 '18 at 08: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? – GuessWho Jan 18 '18 at 08:34

1 Answers1

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

Vao Tsun
  • 47,234
  • 13
  • 100
  • 132