Creating and managing PostgreSQL users

This page describes how to do the following for Cloud SQL instances:

  • Configure the default user account.
  • Create, delete, and update other user accounts.

For information about how users work with Cloud SQL, see PostgreSQL Users.

Before you begin

Before completing the tasks below, create a Cloud SQL instance. For more information, see Creating instances.

If you plan to use your database's administrative client to manage users, you must have configured the default user on the instance. For more information, see Configuring the default account.

Additionally, you must have configured an administrative client connected to your instance. For more information, see Connection Options for External Applications.

Configuring the default user account

When you create a new Cloud SQL instance, you must configure the default user account before you can connect to the instance.

For Cloud SQL for PostgreSQL, the default user is postgres.

To configure the default user:

Console

  1. Go to the Cloud SQL Instances page in the Google Cloud Console.

    Go to the Cloud SQL Instances page

  2. Click the instance to open its Overview page.
  3. Select Users from the Navigation menu.
  4. Find the postgres user and select Change password from the more actions menu More actions icon..
  5. Provide a strong password that you can remember and click Ok.

gcloud

Set the password for the default user:

gcloud sql users set-password postgres \
    --instance=[INSTANCE_NAME] --prompt-for-password

REST

The following request uses the users:update method to update the postgres user.

gcloud auth login
ACCESS_TOKEN="$(gcloud auth print-access-token)"
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
     --header 'Content-Type: application/json' \
     --data '{"name": "postgres", "password": "[PASSWORD]"}' \
     -X PUT \
     'https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[INSTANCE_NAME]/users?name=postgres&'

Creating a user

To create a user:

Console

  1. Go to the Cloud SQL Instances page in the Google Cloud Console.

    Go to the Cloud SQL Instances page

  2. Select the instance to open its Overview page.
  3. Select Users from the Navigation menu.
  4. Click Create user account.
  5. In the Create user account dialog, specify:
    • A User name.
    • A Password.
  6. Click Create.

Users created using Cloud SQL have the privileges associated with the cloudsqlsuperuser role: CREATEROLE, CREATEDB, and LOGIN. A user created by these users can have database connection privileges, which can prevent the user from being deleted. If you need to change the attributes for a user, use the ALTER ROLE command in the psql client. Not all attributes can be modified with ALTER ROLE. Exceptions include the NOSUPERUSER and NOREPLICATION roles.

User name length limits are the same for Cloud SQL as for on-premises PostgreSQL.

gcloud

Create the user:

gcloud sql users create [USER_NAME] \
   --instance=[INSTANCE_NAME] --password=[PASSWORD]

Users created using Cloud SQL have the privileges associated with the cloudsqlsuperuser role: CREATEROLE, CREATEDB, and LOGIN. A user created by these users can have database connection privileges, which can prevent the user from being deleted. If you need to change the attributes for a user, use the ALTER ROLE command in the psql client. Not all attributes can be modified with ALTER ROLE. Exceptions include the NOSUPERUSER and NOREPLICATION roles.

User name length limits are the same for Cloud SQL as for on-premises PostgreSQL.

REST

The following request uses the users:insert method to create a user account 'user_name'.

gcloud auth application-default login
ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
     --header 'Content-Type: application/json' \
     --data '{"name": "[USER_NAME]", "password": "[PASSWORD]"}' \
     -X POST \
     https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[INSTANCE_NAME]/users

Users created using Cloud SQL have the privileges associated with the cloudsqlsuperuser role: CREATEROLE, CREATEDB, and LOGIN. A user created by these users can have database connection privileges, which can prevent the user from being deleted. If you need to change the attributes for a user, use the ALTER ROLE command in the psql client. Not all attributes can be modified with ALTER ROLE. Exceptions include the NOSUPERUSER and NOREPLICATION roles.

User name length limits are the same for Cloud SQL as for on-premises PostgreSQL.

psql Client

  1. At the psql prompt, create the user:
      CREATE USER [USER_NAME] WITH [ATTRIBUTE1] [ATTRIBUTE2]...;
      \password [USER_NAME];
      

    Enter the password when prompted.

    For more information about role attributes, see the PostgreSQL documentation.

  2. You can confirm the user creation by displaying the user table:
      SELECT * FROM pg_roles;
      

Changing a user password

To change a user password:

Console

  1. Go to the Cloud SQL Instances page in the Google Cloud Console.

    Go to the Cloud SQL Instances page

  2. Select the instance to open its Overview page.
  3. Select Users from the Navigation menu.
  4. Click more actions More actions icon. for the user you want to update.
  5. Select Change password, specify a new password, and click OK.

gcloud

Update the password:

gcloud sql users set-password [USER_NAME] \
   --instance=[INSTANCE_NAME] --prompt-for-password

REST

The following request uses the users:update method to update the password for the user account 'user_name'.

gcloud auth login
ACCESS_TOKEN="$(gcloud auth print-access-token)"
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
     --header 'Content-Type: application/json' \
     --data '{"name": "[USER_NAME]", "password": "[PASSWORD]"}' \
     -X PUT \
     'https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[INSTANCE_NAME]/users?name=[USER_NAME]'

psql Client

  1. At the psql prompt, change the password:
    \password [USER];
    

    Enter the password when prompted.

Updating users

To update user properties such as attributes, you must use the psql client. For more information, see Database Roles in the PostgreSQL documentation.

Listing users

To list users:

Console

  1. Go to the Cloud SQL Instances page in the Google Cloud Console.

    Go to the Cloud SQL Instances page

  2. Select the instance to open its Overview page.
  3. Select Users from the Navigation menu.

gcloud

List the users for this instance:

gcloud sql users list --instance=[INSTANCE_NAME]

For a complete list of parameters for this command, see the gcloud sql users list reference page.

REST

The request below uses users:list method to list the users defined for an instance.

gcloud auth login
ACCESS_TOKEN="$(gcloud auth print-access-token)"
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
     -X GET \
     https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[INSTANCE_NAME]/users

psql Client

At the psql prompt, list the PostgreSQL users:

SELECT * FROM pg_roles;

Deleting users

To delete a user:

Before deleting a user, you must drop all the objects it owns or reassign their ownership, and revoke any privileges the role has been granted on other objects.

Console

  1. Go to the Cloud SQL Instances page in the Google Cloud Console.

    Go to the Cloud SQL Instances page

  2. Select the instance to open its Overview page.
  3. Select Users from the Navigation menu.
  4. Click more actions More actions icon. for the user you want to delete.
  5. Select Delete and click OK.

gcloud

Delete the user:

gcloud sql users delete [USER_NAME] --instance=[INSTANCE_NAME]

REST

The following request uses the users:delete method to delete the specified user account.

gcloud auth application-default login
ACCESS_TOKEN="$(gcloud auth application-default print-access-token)"
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
     --header 'Content-Type: application/json' \
     --data '{"name": "[USER_NAME]", "host": ""}' \
     -X DELETE \
     'https://www.googleapis.com/sql/v1beta4/projects/[PROJECT-ID]/instances/[INSTANCE_NAME]/users?host=&name=[USER_NAME]'

psql Client

  1. At the psql prompt, delete the user:
    DROP ROLE [USER_NAME];
    

    For more information about the DROP ROLE statement, see the PostgreSQL documentation.

What's next