Home » Php » php – how to use pg_connect to connect to 2 different postgresql databases on the same computer

php – how to use pg_connect to connect to 2 different postgresql databases on the same computer

Posted by: admin February 23, 2020 Leave a comment

Questions:

I am trying to use 2 instances of postgresql database on the same computer. any idea how should i use pg_connect

I was able to connect to one instance of postgresql using pg_connect successfully. However I have created a second instance for a new web application. How to specify parameter to connect to both databases on the same php code using pg_connect.

How to&Answers:

It is literally the first example given on php.net reference page for pg_connect

$dbconn = pg_connect("dbname=mary");
//connect to a database named "mary"

$dbconn2 = pg_connect("host=localhost port=5432 dbname=mary");
// connect to a database named "mary" on "localhost" at port "5432"

$dbconn3 = pg_connect("host=sheep port=5432 dbname=mary user=lamb password=foo");
//connect to a database named "mary" on the host "sheep" with a username and password

Just change the connection string and declare multiple connection objects and you should be fine