postgres command

The postgres command can be used to connect to a cloudsql postgres database with your personal user It includes subcommands for granting personal access to an instance, setting up a cloudsql proxy, and connecting to the database using a psql shell.

All commands have the following common flags available:

FlagRequiredShortDefaultDescription
namespaceNo-nnamespace set in kubeconfigKubernetes namespace where app is deployed
clusterNo-ccontext set in kubeconfigKubernetes context where app is deployed

Note all flags has to appear before arguments (otherwise the flags will be interpreted as arguments). So the common flags for Postgres needs to be positioned after nais postgres <cmd>, but before arguments:

OK ✅:

Plaintext

Not OK ❌:

Plaintext

Warning

Run the following command first before running any of the other commands:

Plaintext

prepare

Prepare will prepare the postgres instance by connecting using the application credentials and by default modify the permissions on the public schema. All IAM users in your GCP project will be able to connect to the instance.

This operation is only required to run once for each postgresql instance and schema.

bash
ArgumentRequiredDescription
appnameYesName of application owning the database
FlagRequiredShortDefaultDescription
all-privilegesNofalseIf true ALL is granted, else only SELECT is granted
schemaNopublicName of the schema to grant access to

revoke

Revokes the privileges given to the role cloudsqliamuser in the given schema (default public). Does not remove access for users to log in to the database or the roles/cloudsql.admin given to the user in GCP console.

This operation is only required to run once for each postgresql instance.

bash
ArgumentRequiredDescription
appnameYesName of application owning the database
FlagRequiredShortDefaultDescription
schemaNopublicName of the schema to revoke access from

grant

Grant yourself access to a Postgres database.

This is done by temporarily adding your user to the list of users that can administrate Cloud SQL instances and creating a database user with your email.

This operation is only required to run once for each postgresql database.

bash
ArgumentRequiredDescription
appnameYesName of application owning the database

proxy

Update IAM policies by giving your user a timed sql.cloudsql.instanceUser role, then start a proxy to the instance.

bash
ArgumentRequiredDescription
appnameYesName of application owning the database
FlagRequiredShortDefaultDescription
portNo-p5432Local port for cloudsql proxy to listen on
hostNo-HlocalhostHost for the proxy

Note When using proxy to connect to the database, the auth method is username and password. The username is your full Google account email: e.g. ola.bruker@nais.io, and password is blank.

psql

Create a shell to the postgres instance by opening a proxy on a random port (see the proxy command for more info) and opening a psql shell.

bash
ArgumentRequiredDescription
appnameYesName of application owning the database
FlagRequiredShortDefaultDescription
verboseNo-VfalseVerbose will print proxy log

users add

Adds a user to the database By default the user is granted select privileges to the database public schema The privilege level can be altered with the --privilege flag.

bash
ArgumentRequiredDescription
usernameYesName of the new database user
passwordYesPassword for the new database user
appnameYesName of application owning the database
FlagRequiredShortDefaultDescription
privilegeNoselectThe privilege level the user is granted

users get

Get all users in a database.

bash
ArgumentRequiredDescription
appnameYesName of application owning the database

password rotate

Rotate the Postgres database password, both in GCP and in the Kubernetes secret.

bash
ArgumentRequiredDescription
appnameYesName of application owning the database

migrate

Status: Beta

We believe that migration works as intended, but it needs a broader audience to be battle-tested properly. Please report any issues to the #nais channel on Slack.

Commands used for migrating to a new postgres instance.

See also Migrating to a new SQLInstance

All the migrate subcommands require the following arguments:

ArgumentRequiredDescription
appnameYesName of application owning the database
new-instance-nameYesName of the new postgres instance to migrate to

migrate setup

Setup will create a new (target) instance with updated configuration, and enable continuous replication of data from the source instance.

bash

Setup supports the following optional flags:

FlagDescription
tierTier of new instance. See Postgres reference.
typePostgres version of new instance. See Postgres reference.
disk-sizeDisk size of new instance.

These flags must be specified before arguments, e.g:

bash

migrate promote

Promote will promote the target instance to the new primary instance, and update the application to use the new instance.

bash

migrate finalize

Finalize will remove the source instance and associated resources after a successful migration.

bash

migrate rollback

Rollback will roll back the migration, and restore the application to use the original instance.

bash