I am using the following connection to connect to Postgres SQL without Database as I need to fetch all the database names later for configuration
try{
$this->connection = new \PDO($this->database.":host=".$this->host,$this->user,$this->password);
$this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return $this->connection;
}catch(\PDOException $e){
echo $e->getMessage();
}
But I am getting the following error
SQLSTATE[08006] [7] FATAL: database "admin" does not exist
The following are the values that are set in the properties:
$this->database = pgsql
$this->host = localhost
$this->user = admin
$this->password = admin
Can anyone please help me out how to connect to Postgres SQL without any database selection with PHP PDO
Most Postgres servers have three databases defined by default: template0 , template1 and postgres . template0 and template1 are skeleton databases that are or can be used by the CREATE DATABASE command. postgres is the default database you will connect to before you have created any other databases.
There are tree options:
Please, be aware that connection to the database requires permission. Here is explanation what that templates are. You can look for postgresql default database
. Which is quite similar topic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With