|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74767 pg_connect connects to another database
Submitted: 2017-06-16 14:38 UTC Modified: -
Avg. Score:4.8 ± 0.4
Reproduced:6 of 6 (100.0%)
Same Version:0 (0.0%)
Same OS:5 (83.3%)
From: ivnpro at mail dot ru Assigned:
Status: Open Package: PostgreSQL related
PHP Version: 7.0.20 OS: Debian Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: ivnpro at mail dot ru
New email:
PHP Version: OS:


 [2017-06-16 14:38 UTC] ivnpro at mail dot ru
pg_connect gives connection resourse to incorrect database.
php.ini default
postgreSQL 9.5 default

using PGSQL_CONNECT_FORCE_NEW help avoid this bug, but too many connections are opened in production

PHP (cli) (built: Jun 14 2017 05:55:23) ( NTS )

Test script:

try {
    $count = 0;
    while ($count < 1000) {
        $names = ['test1', 'test2', 'test3', 'test4'];
        $name = $names[mt_rand(0,3)];

        $resource = pg_connect("user=postgres dbname=$name host=db-host port=5432");
        if (is_resource($resource) && pg_dbname($resource) !==  $name) {
             * got it !!!
            echo "count $count \n";
            echo "resource $resource\n";
            echo "is_resource ".is_resource($resource)."\n";
            echo "pg_dbname " .pg_dbname($resource). "\n";
            echo "name $name\n";
} catch (Exception $e) {
    echo $e . "\n";

Expected result:
empty output if everything correctly, or exception in case couldn't connect.

Actual result:
count 26
resource Resource id #23
is_resource 1
pg_dbname test3
name test2

that mean asking for connection to test2, but it gives test3


Add a Patch

Pull Requests

Add a Pull Request

PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Oct 30 20:01:24 2020 UTC