Postgresql – How to manage SSL credentials for multiple Postgres connections

postgresqlssl

We're setting up Redash and need to connect to multiple Data Sources that are Postgres databases that require SSL connections. I have a postgres.key and postgres.crt file from each database. Redash is running as user redash on Ubuntu 16.04.

What should I do with these keys so that Redash (and the Python psycopg2 library, behind the scenes) can find the proper credentials to connect to the Data Sources?

The best I've been able to do is put one set of them in /home/redash/.postgresql, which is where psycopg2 looks for credentials by default. However, that doesn't allow me to connect to the second database.

Best Answer

use the sslcert and sslkey paramterer in the connection string to indicate the files you want.

https://www.postgresql.org/docs/10/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS

eg:

 con=psycopg2_connect(dsn='host=host1 user=yourname sslcert=host1.crt sskkey=host1.key')

probably something using string::format would be more practical.

.

If that seems too messy

Using a service definition to store the parameters is another option define the connection paramaters (like above) for each server in pg_services.conf (create that file) and use connection strings like

con=psycopg2_connect(dsn='service=host1')

https://www.postgresql.org/docs/10/static/libpq-pgservice.html

other clients (like psql) can also use the services thus defined, so this way will save you typing in the future.

but it seems redash is configured using environment variables with the database as as a URL perhaps like this:

like this with a service definition

export REDASH_DATABASE_URL=postgres:///?service=database1

or

like this explicitly:

export REDASH_DATABASE_URL="postgres://db_user@host.name.or.ip/db_name?sslcert=host1.crt&sskkey=host1.key"

from a bries sfan of the docs redash seems to use postgres for db_user and db_name

so far as I know libpq does not use a different interface for database names, connection strings, and database urls, where one can be used any other form can be used instead, however the redash docs say to use a URL.

https://www.postgresql.org/docs/10/static/libpq-connect.html#LIBPQ-CONNSTRING