Postgres workload credentials ΒΆ

When an Application or Job uses a Postgres database, Postgres provides connection settings as environment variables. Workloads authenticate with TLS client certificates; no password is provided.

Environment variables ΒΆ

Each credential set contains these variables:

VariableValue
PGHOSTDatabase hostname
PGPORTDatabase port
PGDATABASEDatabase name
PGUSERDatabase username
PGSSLMODETLS verification mode
PGSSLCERTPath to the workload's client certificate
PGSSLKEYPath to the workload's PEM client key
PGSSLROOTCERTPath to the CA certificate
PGJDBCURLJDBC URL for pgJDBC, including username and TLS settings

PGJDBCURL uses sslmode=verify-full and includes paths to the CA certificate, client certificate and PKCS#8 DER key.

Certificate and key files ΒΆ

Each credential set has files under /var/run/secrets/nais.io/postgres/<postgres>/<credential>/, where <credential> is admin, read or readwrite:

FileFormat and use
tls.crtPEM client certificate
tls.keyPEM private key, referenced by PGSSLKEY
tls.pk8The same private key in unencrypted PKCS#8 DER format, referenced by PGJDBCURL

The shared CA certificate is at /var/run/secrets/nais.io/postgres/<postgres>/ca.crt.

Roles and prefixes ΒΆ

The role controls which credential sets the workload receives:

RoleCredential prefixesIntended use
adminUnprefixed and READWRITE_Schema migrations and ordinary application traffic
readwriteREADWRITE_Ordinary application traffic
readREAD_Read-only application traffic

admin is the default role.

For example, a readwrite credential has variables such as READWRITE_PGHOST and READWRITE_PGJDBCURL.

When you set envPrefix, it is prepended before the role prefix. With envPrefix: MYDB_, a readwrite credential has variables such as MYDB_READWRITE_PGJDBCURL.

When envPrefix is not set, PostgreSQL clients that support standard PG* environment variables work out of the box with the unprefixed admin credentials. Use the READWRITE_ credentials for the long-running application process when the workload also runs schema migrations.