0

in my controllers folder I have a file Migrate.php with the following method :

function drop_db($db_name){

    if ($this->dbforge->drop_database($db_name))
    {
        echo "Database {$db_name} deleted!";

    }
}

When called I need it to drop the database, however I get the following error:

An uncaught Exception was encountered

Type:        ErrorException
Message:     pg_query(): Query failed: ERROR:  cannot drop the currently open database

I tried to close the connexion using $this->db->close(); at the beginning of my method but I still get the same error!

6
  • 1
    Did you try connecting to another database? as suggested here: stackoverflow.com/questions/36502401/… Commented Dec 21, 2017 at 22:03
  • 2
    As @MehdiBounya noted, you'll need to connect to another db, and a typical db to connect to for administrative actions is the "admin" postgres database that should be created by default in your Postgres cluster. Commented Dec 21, 2017 at 22:15
  • @bma can I switch/connect to another database on the fly, just before executing $this->dbforge->drop_database($db_name) ? Commented Dec 22, 2017 at 16:22
  • Nothing on the Postgres side prevents changing databases on the fly. You'll need to look at the PHP connection docs, but I can't imagine that it would be difficult to do. Commented Dec 22, 2017 at 16:40
  • @bma I am asking if there is a codeigniter way to abstract that, am creating a library and I would like to support not only postgres but other db drivers Commented Dec 22, 2017 at 16:43

0

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.