nais postgres proxy
nais postgres proxy POSTGRES [flags]
Request personal access and listen on loopback. If --reason is omitted, prompt for an audit reason interactively.
PostgreSQL clients must verify the server certificate. Use nais postgres psql for automatic TLS setup.
Use --output env for POSIX shell-quoted PostgreSQL environment variable assignments on stdout. Use --output json for a JSON object with the same variable names as keys and string values, without shell quoting. Both formats send progress and warnings to stderr. After writing the settings, stdout is closed so consumers can read to EOF without waiting for the proxy to stop. EOF alone does not indicate success; consumers must validate the settings and handle setup failures. PGHOST is the TLS server name; PGHOSTADDR and PGPORT point to the local relay. PGDATABASE is always app, the application database. PGPASSWORD is omitted unless --print-password is set.
Keep the proxy running while using these settings. The temporary PGSSLROOTCERT file is removed when it stops. Use --exit-on-stdin-eof to stop the proxy gracefully when stdin reaches EOF after setup. A program managing the proxy can keep its stdin pipe open while using the connection, then close it to stop the proxy and clean up. Without this flag, stdin EOF does not stop the proxy. With it, stdin connected to /dev/null causes the proxy to stop immediately after setup.
Source the env output with auto-export enabled (set -a) to pass the variables to child processes. Programs consuming json output can read to EOF and decode the object while keeping the proxy’s stdin pipe open until they are finished using the connection. Inherited PostgreSQL settings such as PGSERVICE may need to be unset.
Options
--access-level- Access level: read, write, admin (application database owner privileges), or maintain (instance monitoring and maintenance).
--exit-on-stdin-eof- Stop the proxy when stdin reaches EOF after setup (disabled by default).
--host- Local loopback address (default 127.0.0.1).
-o,--output- Format output (human, env, or json).
-p,--port- Local port (default random).
--print-password- Print the database password to stdout (sensitive).
--reason- Reason for personal access (at least 10 characters); prompted if omitted interactively.
--ttl- Requested lifetime (default 30m, maximum 1h).
Inherited options
--config- Specify the
PATHto the configuration file. --confirm-login- Ask for confirmation before starting required login flows. Interactive terminals only.
-e,--environment- Select or filter by Nais
ENVIRONMENT, for example dev-gcp or prod-gcp. Overrides the environment configured with nais defaults set environment. --no-colors- Disable colors in the output.
-t,--team- Select the Nais
TEAMthat owns the resources. Overrides the team configured with nais defaults set team. -v,--verbose- Set verbosity level. Use -v for verbose, -vv for debug, -vvv for trace.
Examples
# Write connection settings to a file while the proxy runs. In another terminal, use 'set -a; . ./postgres.env; set +a' to export them. Use a private file (umask 077) when adding --print-password.
$ nais postgres proxy my-postgres -t my-team --reason 'Investigating issue' --output env > postgres.env
# Emit connection settings as JSON for a program to consume. Add --print-password only when the program needs the password.
$ nais postgres proxy my-postgres -t my-team --reason 'Investigating issue' --output json
# Let a supervising program stop the proxy by closing its stdin pipe after consuming the JSON connection settings.
$ nais postgres proxy my-postgres -t my-team --reason 'Investigating issue' --output json --exit-on-stdin-eof