How to connect to a Postgres database without specifying a database name in PDO?

I use the following connection to connect to Postgres SQL without a database, since I need to get all 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 get the following error:

 SQLSTATE[08006] [7] FATAL: database "admin" does not exist 

The following are the values ​​specified in the properties:

 $this->database = pgsql $this->host = localhost $this->user = admin $this->password = admin 

Can anyone help me find out how to connect to Postgres SQL without selecting a database with PHP PDO

+7
php pdo postgresql
source share
6 answers

Since you always have a default database called postgres on PostgreSQL, you can connect to it by default as follows:

 $dbh = new PDO($this->database.":host=".$this->host.";dbname=postgres",$this->user,$this->password); 

Then you will do another one after the good one.

0
source share

There are tree options:

  • Postgres
  • template0
  • template1

Please note that permission is required to connect to the database. It explains what these templates are. You can search for postgresql default database . This is a completely different topic.

0
source share

This answer explains it well:

fooobar.com/questions/128810 / ...

You need to explicitly point to one database, which is always guaranteed, as described above, namely "postgres": $this->connection = new PDO($this->database.":host=".$this->host.";dbname=postgres",$this->user,$this->password);

... or, if necessary, set $this->dbname = postgres and then $this->connection = new PDO($this->database.":host=".$this->host.";dbname=".$this->dbname,$this->user,$this->password);

0
source share

For what I understood from the error message, it seems that your DSN is wrong ( admin , which is your username and / or password, is interpreted as the database name) ...

0
source share

In addition to the current excellent answers, we also consider creating db for each unix user (or at least for the admin user), so the psql command works without parameters, and you can conveniently use it for interactive use.

0
source share

Postgresql is unable to connect to the database server without specifying an existing database.

The reason you get this error:

 SQLSTATE[08006] [7] FATAL: database "admin" does not exist 

is that if you do not specify a database name, by default it will try to connect to the database with the same name as the user you specified (see the description of the dbname connection parameter).

When postgresql is first initialized, three default databases are created:

  • postgres - designed for a general-purpose database for the postgres admin user to access
  • template0 - Main template - should not be changed after initialization
  • template1 - default template - used to create all new databases from

For more information about them, see Template Databases .

If you want to get a list of available databases, you will need to connect to one of the above databases.

I would usually use template1 for this, because theoretically the postgres database could be deleted, and it would be dangerous to connect to the template0 database if you accidentally change it. Since template1 used as the default template when creating databases, it is likely to exist.

Also note that any user with whom you connect must have permission to connect to this database. All three of these databases are owned by the postgres user, which means that if you do not connect using this user, you need to make sure that the user you are using has permission to access the database.

0
source share

All Articles