php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72544 pg_connect using the "service" parameter
Submitted: 2016-07-05 02:19 UTC Modified: 2016-07-12 05:15 UTC
From: floriparob at gmail dot com Assigned:
Status: Not a bug Package: PostgreSQL related
PHP Version: 5.6.23 OS: Debian
Private report: No CVE-ID: None
 [2016-07-05 02:19 UTC] floriparob at gmail dot com
Description:
------------
The documentation for pg_connect says that it recognises the "service" parameter as a connect string.
In the pg_service.conf file you can have entries for several databases, each with their own distinct IP addresses, passwords, etc. So, in your PHP code you'd expect to be able to do something like this wrapped up in a try catch block:-

$connect_string = "service=staging";
$pg_handle = pg_connect($connect_string);

to access the "staging" data base.

With psql you have to do this:-

PGSERVICE=staging;export PGSERVICE
psql

is run without any parameters. It opens the pg_service.conf file and finds the entry defined as "staging" and from that obtains all the necessary configuration parameters in order to open a connection to that database.

I tried this:-

putenv("PGSERVICE=staging");
$connect_string = "service=staging";
$pg_handle = pg_connect($connect_string);

but still no luck.

So, if this is not a bug could somebody please enlighten me about the correct way to connect using pg_service.conf.






Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-05 07:29 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2016-07-05 07:29 UTC] requinix@php.net
It seems to be supported as far as I can tell. What's the error message?
 [2016-07-06 22:09 UTC] floriparob at gmail dot com
-Status: Feedback +Status: Open
 [2016-07-06 22:09 UTC] floriparob at gmail dot com
The error message is:-

PHP Warning:  pg_connect(): Unable to connect to PostgreSQL server: definition of service "staging" not found

If it were a permissions issue I'd expect to see "cannot stat" etc. as the error message.

I had a look at the libpq source, in particular fe_connect.c, and it requires the environment variables PGSERVICE and PGSERVICEFILE in order to connect, obtaining the "key" from PGSERVICE and then looking up the connect parameters in pg_service.conf whose path is defined in PGSERVICEFILE. This works with psql.

So how does php5-pgsql set the PGSERVICE environment variable?

Should we upgrade to PHP7?
 [2016-07-07 05:30 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2016-07-07 05:30 UTC] requinix@php.net
Make sure you're looking at the pg_service.conf file located somewhere within /etc/postgresql and not one in another directory, like your home directory. Or possibly in the home directory of whatever user PHP is running as.
It can also vary with the PGSERVICEFILE or PGSYSCONFDIR environment variables.

> So how does php5-pgsql set the PGSERVICE environment variable?
It does not. The environment variables are fallback behavior for information not provided in the connection string, so if you try to connect with "service=staging" then libpq won't look for a PGSERVICE.

The connection string is passed to PQconnectStart (if async) or PQconnectdb without much modification.
https://www.postgresql.org/docs/current/static/libpq-connect.html

> Should we upgrade to PHP7?
Shouldn't matter for this. PHP 5.6 is still in active support so 5.6.x and 7.0.y will be more or less equal in terms of bug fixes and such.
 [2016-07-12 01:01 UTC] floriparob at gmail dot com
-Status: Feedback +Status: Open
 [2016-07-12 01:01 UTC] floriparob at gmail dot com
After reading your reply it functions as follows:-

putenv("PGSERVICEFILE=/path/to/your/service/file/pg_service.conf");
$conn_string = "service=testdb";
try {
   $pgconn_handle = pg_connect($conn_string);
 . . . etc.

You need to set PGSERVICEFILE to the complete path and file name.
The file has to be readable by apache.
I'll add some comments about this to the pg_connect documentation.

This report may be closed.
 [2016-07-12 05:15 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-07-12 05:15 UTC] requinix@php.net
Alright, then I'll chalk this up to a misconfigured environment.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 10 23:01:27 2024 UTC