This is the multi-page printable view of this section. .
exec and psql
Two ways to run SQL against an instance: pg exec for one-shot SQL or container commands, pg psql for interactive sessions.
pg exec
SQL mode (default)
Arguments without -- are executed as SQL via psql, using the instance’s configured user and database.
Container command mode (after –)
Arguments after -- are run directly inside the container (as root).
Remote database (–dsn)
Execute SQL against any database reachable via a connection string, using a temporary container. --dsn only supports SQL mode; container commands require a local instance.
pg psql
Interactive session
Inside the shell you get full psql features: SQL with history and tab completion, meta-commands (\dt, \du, \l), and \q to quit.
Non-interactive (scripts)
Switch to postgres superuser
Some administrative tasks (e.g., creating certain extensions, modifying system-level settings) require postgres superuser privileges. Use -- to pass psql arguments and switch user:
Recommended approach: Use the instance default user (admin) for daily operations, and switch to postgres only when superuser privileges are needed. This is safer and more convenient than modifying config files or restarting containers.
Example scenarios:
Remote database (–dsn)
Rules
--dsnand--instanceare mutually exclusive: the connection string determines host, port and database, so-iis rejected to avoid silent misuse.- With
--dsn, the database is the path part of the URL:postgres://user:pass@host:5432/mydbconnects tomydb. To use another database, change the path. - With a local instance,
--passes raw psql arguments (including-d/-U), overriding the instance defaults.