Connect with psql
Open a psql session on a Managed PostgreSQL service, first with the ready-made connection URI,
then with full certificate verification for production use.
Prerequisites
You need:
- A running Managed PostgreSQL service.
- The Exoscale CLI, configured with your API credentials.
psql, from the PostgreSQL client package of your system:postgresql-clienton Debian and Ubuntu,postgresqlon Fedora and RHEL,libpqon Homebrew. Use apsqlat least as recent as the server major version, otherwise some\doutput is incomplete.jq, for the certificate verification steps.- Your client IP address in the service IP filter. See Restrict Connections to a Service.
Connect
Pass the connection URI of the service to psql:
psql "$(exo dbaas show SERVICE_NAME -z ZONE --uri)"Parameters:
SERVICE_NAME: name of your Managed PostgreSQL service, for examplemy-postgresql.ZONE: Exoscale zone the service runs in, for examplech-gva-2.
Keep the quotes: the URI contains characters such as ? that the shell would otherwise interpret.
Once connected, confirm which server you reached:
SELECT version(), current_database(), current_user;To read the URI itself, for example to configure an application, run the inner command on its own:
exo dbaas show SERVICE_NAME -z ZONE --uriIt has this shape:
postgres://avnadmin:PASSWORD@SERVICE_HOST:PORT/defaultdb?sslmode=requireThe port is assigned per service and is not 5432. Read it from the CLI instead of hardcoding it.
Caution
The URI contains the avnadmin password in clear text. Keep it out of shell history, tickets
and CI logs. The command substitution above passes it to psql without printing it.
Choose an SSL Mode
Managed PostgreSQL accepts TLS connections only, so sslmode=disable is always rejected.
sslmode controls how much of the server certificate the client checks:
sslmode | Encrypts traffic | Verifies the CA | Verifies the hostname | Needs the CA certificate |
|---|---|---|---|---|
require | Yes | No | No | No |
verify-ca | Yes | Yes | No | Yes |
verify-full | Yes | Yes | Yes | Yes |
The URI returned by the CLI uses sslmode=require. It encrypts the connection but accepts any
certificate, so it does not protect against a man-in-the-middle. For production workloads use
verify-full, which also checks that the certificate was issued by your organization’s CA for the
host you connect to.
Connect with Certificate Verification
Retrieve the CA Certificate
verify-ca and verify-full need the CA certificate that signed the server certificate. Save it
in PEM format:
exo dbaas ca-certificate -z ZONE > ca.pemParameters:
ZONE: Exoscale zone, for examplech-gva-2.
The CA belongs to your organization, not to a service: the same ca.pem validates every Database
Service of the organization, in every zone.
Export the Connection Fields
The URI already contains sslmode=require, so build the connection string from the individual
fields instead of editing the URI. Export them first:
eval "$(exo dbaas show SERVICE_NAME -z ZONE -O json | jq -r '.pg.uri_params | "export PGHOST=\(.host) PGPORT=\(.port) PGDATABASE=\(.dbname) PGUSER=\(.user)"')"
export PGPASSWORD="$(exo dbaas user reveal-secrets SERVICE_NAME avnadmin -z ZONE -O json | jq -r .password)"Parameters:
SERVICE_NAME: name of your Managed PostgreSQL service, for examplemy-postgresql.ZONE: Exoscale zone the service runs in, for examplech-gva-2.
The password is read separately because the JSON output of exo dbaas show does not include it.
Connect with Full Verification
psql "host=$PGHOST port=$PGPORT dbname=$PGDATABASE user=$PGUSER sslmode=verify-full sslrootcert=CA_PATH"Parameters:
CA_PATH: path to the CA certificate saved in the first step, for exampleca.pem.
psql reads PGPASSWORD from the environment. A relative sslrootcert is resolved from the
current directory, so use an absolute path in scripts and services.
Connect using the hostname returned by exo dbaas show. A CNAME of your own is not in the server
certificate and fails with a hostname mismatch. The node IP address is in the certificate, but it
changes when the node is replaced, so do not rely on it.
Store the Password in a Password File
To keep the password out of your environment, store it in ~/.pgpass, one line per service, in
the format hostname:port:database:username:password:
SERVICE_HOST:PORT:*:avnadmin:PASSWORDlibpq ignores the file if other users can read it, so restrict its permissions:
chmod 0600 ~/.pgpassConnect Through a Connection Pool
Every service also has a PgBouncer endpoint, on a port of its own. Connection pools are created
through the API, not with exo dbaas:
echo '{"name":"POOL_NAME","database-name":"DATABASE","mode":"transaction","size":10}' | \
exo api create-dbaas-pg-connection-pool SERVICE_NAME -z ZONEParameters:
POOL_NAME: name of the pool, for exampleapp-pool. Clients use it as the database name.DATABASE: the database the pool connects to, for exampledefaultdb.mode:session,transactionorstatement.size: number of server connections the pool keeps open.
Read the pool URI back:
exo api get-dbaas-service-pg SERVICE_NAME -z ZONE -q '"connection-pools"'The connection-uri of the pool points to the PgBouncer port and uses the pool name as database
name. A pool created as above, without a username, has no credentials in its URI: connect with
the user and password of the service, for example:
psql "host=$PGHOST port=POOL_PORT dbname=POOL_NAME user=$PGUSER sslmode=require"Parameters:
POOL_PORT: the port in the poolconnection-uri.
A pool created with a username field has that user’s password in its connection-uri, so keep
the output of exo api get-dbaas-service-pg out of logs and tickets as well.
Useful First Commands
Meta-commands to explore the service once connected:
\l list databases
\dn list schemas
\du list roles
\d list relations in the search path
\x toggle expanded output, useful for wide rows
\conninfo show the current connection, including its SSL status
\q quitThe _aiven database listed by \l is used by the platform. Do not connect to it or change it.
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
timeout expired, or the command hangs | Your client IP is not in the service IP filter. | Add it, see Restrict Connections to a Service. |
no pg_hba.conf entry for host "...", user "...", database "...", no encryption | The client connected without TLS, for example with sslmode=disable. | Set sslmode to require at minimum, and verify-full in production. |
root certificate file "..." does not exist | sslrootcert points to a missing file, or is relative and the process runs from another directory. | Use an absolute path to ca.pem. |
SSL error: certificate verify failed | sslrootcert points to a file that is not the CA of your organization. | Save the CA again with exo dbaas ca-certificate. |
server certificate for "..." does not match host name | With verify-full, you connected through a name that is not in the certificate, such as your own CNAME. | Use the hostname from exo dbaas show. |
FATAL: password authentication failed | Wrong password, for example after a password reset. | Read it again with exo dbaas user reveal-secrets. |
FATAL: no such database on the PgBouncer port | You used the database name on the PgBouncer port. | Use the pool name as database name. |
sorry, too many clients already | The connection limit is reached. | Check SHOW max_connections; and use a connection pool. |