larskanis/rails — pull request #1 by larskanis (2012-01-05 08:51:56+00:00)
Title: PostgreSQL: Allow setting of any libpq params in database connection specification

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.