Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

PostgreSQL: Allow consistent setting of libpq params in database connection specification #4315

merged 3 commits into from Jan 5, 2012


None yet
4 participants

larskanis commented Jan 5, 2012

The PostgreSQL connection adapter is based on pg.gem (for two years now), which uses libpq to connect to the server. Several connection parameters can be given to PGconn and so to libpq, but only some parameters can be used in ActiveRecords connection specification. Furthermore libpq inspects several environment variables for setting default values. The list of parameters varies between different PostgreSQL versions.

Current behavior of connection settings for adapter postgresql is inconsistent. Some connection parameters can be set by the connection specification only (:port, :database) but not per environment variable (PGPORT, PGDATABASE). Some parameters can be set by both (:host/PGHOST, :username/PGUSER, :password/PASSWORD) and some parameters can only be set per env (PGOPTIONS, PGSSLKEY, PGSERVICE, ...).

On the other hand, using environment variables is quite difficult, if one needs different connections to several databases at the same time. It conflicts with the connection pooling and on demand connectivity, since ActiveRecords ConnectionSpecitfication doesn't take these settings into account.

The attached patch passes all parameters not recognized by ActiveRecord through to PGconn, so every (possibly future) libpq parameter can be used in the connection specification. Furthermore it updates the documentation and adds a test case for setting a specific libpq parameter. I did not add a test case for using environment variables, because I guess it's out of scope of ActiveRecord.

@tenderlove tenderlove added a commit that referenced this pull request Jan 5, 2012

@tenderlove tenderlove Merge pull request #4315 from larskanis/allow_setting_of_pgconn_params
PostgreSQL: Allow consistent setting of libpq params in database connection specification

@tenderlove tenderlove merged commit 351a600 into rails:master Jan 5, 2012

Thanks for fixing this. I wasted a few hours trying to deploy a simple Sinatra app to Heroku that refused to connect to the shared database.

I kept getting this error but couldn't figure out why:
ActiveRecord::StatementInvalid: PGError: ERROR: invalid value for parameter "client_min_messages": ""

Since I wasn't setting a value for :min_messages, it was being set to "", and hence the error.


larskanis replied Jan 8, 2012

:min_messages isn't touched by this commit - only the values that are used for PGconn.connect. But you're right, also for the other parameters, nil values should be handled equal to not defined keys for consistency.

This commit seems to change some previously working behavior where the connection_parameters could have a'socket parameter. This seems to have changed to reuse host. I was unable to find this documentation in the change log. This change might be worth documenting.


larskanis replied Sep 26, 2012

The mysql adapter has a socket parameter, but AFAIK the postgresql adapter always used host.

Im sorry I didn't explain the issue I am having well, and the more I think about it my thought about adding documentation for this change doesn't make sense. So feel free to disregard. Thanks for replying quickly, and allowing the passing of additional params to PGconn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment