existing logic produces connect strings that look like this
user=someuser dbname=somedatabase password= host=someserver

my Postgres is unhappy with that "password= " bit, and does not connect.

i'm using debian stable (sarge) which is postgresql 7.4.7-6sarge3, php 4.3.10-18

So, here's a patch, that removed the password when it's not set.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Shiny’s picture

Title: pg_connect string in install.pgsql.inc fails to connect on blank password » Patch attached - pg_connect string in install.pgsql.inc fails to connect on blank password
Shiny’s picture

Title: Patch attached - pg_connect string in install.pgsql.inc fails to connect on blank password » pgsql - pg_connect string in install.pgsql.inc fails to connect on blank password
Version: 5.1 » 6.x-dev
webchick’s picture

MySQL allows connection without password, so it makes sense pgsql would as well.

Shiny’s picture

yes indeed. IP based authentication - or simple "trust" authentication, on trusted networkds, is common in postgresql.

ned0r’s picture

I've discovered the same problem, I wrote a patch, and was going to file a bug, but found this one (with an almost identical patch).

Is there any plan to put this patch into version control?

I tested it using Postgres 8.1, PHP5 in a Linux VServer

Dries’s picture

The use of isset() looks inconsistent. Care to double-check that?

Shiny’s picture

i found that pass was always isset()==true, and set to an empty string.
Something earlier (database "url" parser maybe) always sets the value.

Dries’s picture

Strange. Why couldn't the port number be an empty string? Why would $port be different from $pass? Is that a weirdity in PHP's parse_url()? It would be appreciated if we could investigate this a bit more.

Shiny’s picture

database.pgsql.inc does not have this problem

Why have the same code in 2 places? seems it could be pushed into one place.
I need to patch for all my live drupal5 sites - any chance of accepting it there? and a more extensive patch goes against Drupal6? perhaps putting the pg_connect call in one place instead of two.

Shiny’s picture

FileSize
1.74 KB

Alternate patch -- this one include( database.pgsql.inc ), and then calls existing db_connect($url);
The code there handles isset() correctly.

Tested on Debian Sarge, php4, postgresql 7.4
and on Ubuntu Edgy with Php5 and postgresql 8.2

regarding the port isset, i probably had a stray colon there. my bad. can't replicate again.

znikke’s picture

I think this is only a partial solution. The "standard" way to connect to a postgresql database running on the current host is to simply do
pg_connect("user=dbuser dbname=databasename") which will use the default access method, which is usually to connect to the Unix Domain Socket which supports reliable identfication (no silly passwords).

The problem with this "parse the url approach" is that I can't produce such a conn-string, nor can I produce a conn-string that looks like "host=/var/run/postgresql user=dbuser dbname=databasename" (which would also work) since the slashes messes things up.

So, I ended up doing the following:
In my settings.php:
$db_url = "pgsql://user=dbuser dbname=databasename";

And patching database.pgsql.inc to say:
$connection = @pg_connect(urldecode($url['host']));

This solves all problems for me since it doesn't try to be intelligent and thus destroy things.

In order to solve all these kinds of problems I would suggest implementing a simple check to see if the host-string from parse_url contains the string "user=" and if that's the case pass it through unmodified to pg_connect. Or provide an example of how to do this.

Requiring the use of connecting to localhost, passwords, etc is just silly when there has been a good method of connecting to the postgresql server for quite some time now. The installation instructions should be updated to reflect this (connecting to localhost instead of the unix domain socket is almost always wrong).

Shiny’s picture

perhaps we don't want a string at all
an array..

if (!is_array($url)) {
  $url = parse_url($url);
}

this will give complete control of all attributes, while not requiring mysql users to change anything.

It also means that, if drupal ever supports another database with it's own unique connection requirements, they will be supported.

Dries - do you approve?? If so i'll make another patch.

Shiny’s picture

to be clearer, i'm proposing that settings.php like more like this

#pgsql://drupal:5433@localhost/drupalfive';
$db_url['dbtype'] = 'psql';
$db_url['username'] = 'drupal';
$db_url['host'] = 'localhost';
$db_url['dbname'] = 'drupalfive';
$db_url['post'] = '5433';
$db_url['password'] = ''; #if you actually have a password of empty string -- otherwise leave as not set.
johnhelen’s picture

When I set up my Drupal application (version 5.1) as the first user and connect with a database (postgres) with an empty password. I got an error "Cannot connect to database .... check username, password...". Apply this path, my problem is solved.
Great job.

Dries’s picture

It would fix many problems if we'd just get rid of the URL and use individual variables as proposed by Shiny.

ax’s picture

Status: Needs review » Needs work

an empty password also throws 2 "undefined index" php notices with mysql:

PHP Notice:  Undefined index:  pass in includes\install.mysql.inc on line 32
PHP Notice:  Undefined index:  pass in install.php on line 148

(CVS HEAD of today, 31-Aug-2007). this would be fixed with individual variables as suggested in #13.

Shiny - are you still listening? i think Dries approved - now will you make another patch? :) a big thank you from me in advance!

Shiny’s picture

ax: still here, using this patch in our own git version.
Will get onto this shortly

ax’s picture

Shiny: great - looking forward to see this going into Drupal!

suzanne.aldrich’s picture

Subscribing. I posted a fix a long time ago for pg_connect at http://drupal.org/node/18586 (explanation is at http://drupal.org/files/issues/debian-bug-295496) to allow connecting to postgresql using unix sockets. There is also recent discussion at http://drupal.org/node/26836 about Socket Connection/path support.

hswong3i’s picture

Assigned: Shiny » hswong3i
Priority: Normal » Critical
Status: Needs work » Needs review
FileSize
1.61 KB

Double check with latest CVS HEAD and found the problem still existing in install.pgsql.inc. This patch backport the handling from current db_connect(), which employ the original bug fix idea. Patch tested for PostgreSQL 8.1.9 on Debian etch 4.0r1 with PHP 5.2.0-8+etch7. Installation is passed with no error message. Should be trivial to commit.

chx’s picture

Status: Needs review » Reviewed & tested by the community

Looks good here.

ax’s picture

Status: Reviewed & tested by the community » Needs work

would it be possible to solve this with the more elegant solution proposed by shiny in http://drupal.org/node/125105#comment-229460 ? no urldecode()s, less code, a +1 from dries (and me) ... tnx!

hswong3i’s picture

Status: Needs work » Reviewed & tested by the community

@ax: that sounds good for me too, but should be another issue. At least my patch is just reusing the current implementation, which should be always correct. Since D6 beta2 is already out, I am really hoping to help about the complete of pgsql implementation, with as less problem as possibile. At least, we should fix this critical bug on time with a simple approach. Just let this in, and further more explore for other possibility?

Moreover, according to my previous research with Oracle/DB2/MSSQL, our current syntax can also works fine with it. I would like to support either: 1) change all database handling as new array style (require for more time and patch with more test), or 2) keep existing syntax plus this patch, but not 3) introduce different syntax between databases (a bit confusing...). Any idea?

Gábor Hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

ax: migrating to a different method for database settings in settings.php is a big change which we are not going to do after Drupal 6.0 beta 2. Although Dries said it is a better solution, that was in April, and there was no patch to migrate to that model when Drupal 6 was open for such changes. Now we are fixing bugs, which hswong3i's patch does exacly. So committed that one.

Feel free to open an issue against Drupal 7 to migrate to an array format for database settings, if you'd like.

hswong3i’s picture

Thanks Gabor :)

Follow up: I just open another issue for Shiny's suggestion (http://drupal.org/node/184917). Maybe we have chance to ship it with D6, maybe we don't but need to wait for D7, I don't really know. BTW, it is a good idea that we should give some love about :)

Anonymous’s picture

Status: Fixed » Closed (fixed)