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

php connect to postgresql
php postgresql pdo connection example

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


As you always have a default database called postgres on PostgreSQL you can By default make a connection to it like this :

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

Then you will make another one after to the good one.

PHP PDO - Connecting to PostgreSQL, Before connecting to the PostgreSQL database server using PHP PDO, you need to You can specify the user name in either DSN or in constructor of the PDO  To connect to a PostgreSQL database, you need to create a new instance of the PDO class. In the connect () method, we read the database configuration parameters in the database.ini file, construct a connection string, and pass it to the PDO constructor. After that, place the following code in the index.php file.


PDO_PGSQL DSN - Manual, PDO_PGSQL DSN — Connecting to PostgreSQL databases If you specify the user name in the DSN, PDO ignores the value of the user name argument in the  PostgreSQL is an open source relational database management system. Psql is an interactive terminal program for working with PostgreSQL. Use psql to edit, automate, and execute queries in PostgreSQL. pgAdmin is a web interface for managing PostgreSQL databases. It provides a visual, user-friendly environment with a host of practical solutions


This answer explains it well:

https://stackoverflow.com/questions/2370525#2411860

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

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

pg_connect - Manual, pg_connect() opens a connection to a PostgreSQL database specified by the a PostgreSQL connection; pg_host() - Returns the host name associated with the PDO('pgsql:user=exampleuser dbname=exampledb password=examplepass'); on console without specifying password) and everyone can connect to db  First, launch psql program and connect to the PostgreSQL Database Server using the postgres user by clicking the psql icon as shown below: Second, enter the necessary information such as Server, Database, Port, Username, and Password. Press Enter to accept the default.


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

Connecting to PostgreSQL using PHP, Connecting to the PostgreSQL from PHP. a. php-gd-7.3.11-1.el7.remi.x86_64 php-pdo-7.3.11-1.el7.remi.x86_64 There are two ways we can connect to the PostgreSQL database: <br>"; echo "<h3>Print First and last name:</h3>"; for ($​row = 0; $row < pg_numrows($result); $row++) { $firstname  The PDO_PGSQL Data Source Name (DSN) is composed of the following elements, delimited by spaces or semicolons: DSN prefix. The DSN prefix is pgsql:. host. The hostname on which the database server resides. port. The port on which the database server is running. dbname. The name of the database. user. The name of the user for the connection.


In addition to the current excellent answers, also, Consider creating a 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.

PHP Cookbook, If you already have an SQLite 2 database, consider using the PDO drivers to migrate to SQLite 3. 10.3 Connecting to an SQL Database Problem You want access to a SQL Without a database, dynamic web sites aren't very dynamic. new PDO('mysql:unix_socket=/tmp/mysql.sock', $user, $password) // PostgreSQL​  Defaults to the PostgreSQL™ standard port number (5432). database. The database name. To connect, you need to get a Connectioninstance from JDBC. To do this, you use the DriverManager.getConnection()method: Connection db = DriverManager.getConnection(url, username, password); Connection Parameters. In addition to the standard connection parameters the driver supports a number of additional properties which can be used to specify additional driver behavior specific to


Beginning PHP and MySQL 5: From Novice to Professional, ODBC v3: Not a database per se, but enables PDO to be used in conjunction with any PostgreSQL: Accessible via the PGSQL driver. connection variables such as the hostname, port, and database name. The username and password parameters specify the username and password used to connect to the database,​  Use the following PHP code to connect to PostgreSQL and select a database. Replace USERNAME with your username, PASSWORD with your password, and DBNAME with the database name: <?php $db_connection = pg_connect ("host=localhost dbname= DBNAME user= USERNAME password= PASSWORD "); ?>.


How to Connect PHP to MySQL Database with PDO and MySQLi, If you are a beginner in website development, knowing how to use PHP script to There are two methods to connect to a MySQL database using PHP: MySQLi, and PDO. types that use PHP, such as MySQL, MSSQL, Informix, and PostgreSQL. You also need the correct server name or hostname for the configuration. To use PDO with your database you need to have the correct PDO driver installed for it. For the SQLite PDO driver you need to configure PHP --with-pdo-sqlite. If you are using a RPM based system there are pdo-sqlite.rpm's available. Before we go any further, lets connect to a database and see what all the fuss is about. Connect to a database


Class Phalcon\Db\Adapter\Pdo\Postgresql, Source on GitHub. Specific functions for the Postgresql database system. <?php use Call it when you need to restore a database connection. Converts bound parameters such as :name: or ?1 into PDO bind params ? <?php print_r( Active SQL statement in the object without replace bound parameters. public array  The name of the class property is the connection name, and can be used while connecting to specify a group name. Some database drivers (such as PDO, PostgreSQL, Oracle, ODBC) might require a full DSN string to be provided.