Managing PostgreSQL users
You can add users, remove users, and manage their individual settings.
Warning
While you can use SQL commands to assign user privileges, you cannot use them to add or modify users. For more information, see Assigning privileges and roles to users.
Getting a list of users
- Navigate to the folder dashboard and select Managed Service for PostgreSQL.
- Click the name of your cluster and open the Users tab.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
To get a list of cluster users, run the following command:
yc managed-postgresql user list \
--cluster-name <cluster_name>
You can get the cluster name from the folder’s cluster list.
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Use the User.List method to execute the following request via cURL
:curl \ --request GET \ --header "Authorization: Bearer $IAM_TOKEN" \ --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/users'You can get the cluster ID from the folder’s cluster list.
-
Check the server response to make sure your request was successful.
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapiBelow, we assume the repository contents are stored in the
~/cloudapi/directory. -
Use the UserService.List call to execute the following request via gRPCurl
:grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/user_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>" }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.UserService.ListYou can request the cluster ID with the list of clusters in the folder.
-
Check the server response to make sure your request was successful.
Creating a user
Warning
Granting the public table create privilege to new users depends on the PostgreSQL version:
- 14 and lower: The privilege is granted automatically and cannot be revoked.
- 15 and higher: The privilege is manually granted to the user.
-
Navigate to the folder dashboard and select Managed Service for PostgreSQL.
-
Click the name of your cluster and select the Users tab.
-
Click Create user.
-
Specify the database user’s name.
The username may contain Latin letters, numbers, hyphens, and underscores but must begin with a letter (except the
pg_combination), number, or underscore. The name may be up to 63 characters long.Such names as
admin,repl,monitor,postgres,mdb_superuser,mdb_admin,mdb_monitor, andmdb_replicationare reserved for Managed Service for PostgreSQL. You cannot create users with these names. -
Select a method for setting a password:
-
Enter manually: Set your own password. The password must be from 8 to 128 characters long.
-
Generate: Generate a password using Connection Manager.
To view the password, navigate to the cluster page, select the Users tab, and click View password for the relevant user. This will open the page of the Yandex Lockbox secret containing the password. Viewing passwords requires the
lockbox.payloadViewerrole. -
-
Select the deletion protection option.
Possible values:
- Same as cluster
- Enabled
- Disabled
-
Choose which databases the user can access:
- In the Database field, click
next to the drop-down list. - Select a database from the drop-down list.
- Repeat these two steps to select all required databases.
- To delete a mistakenly added database, click
next to its name.
- In the Database field, click
-
Configure the DBMS settings for the user.
-
Click Save.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
To create a cluster user, run this command:
yc managed-postgresql user create <username> \
--cluster-name <cluster_name> \
--password=<password> \
--permissions=<database_list> \
--conn-limit=<maximum_number_of_connections>
Where:
-
cluster-name: Cluster name. -
password: User password. The password must be from 8 to 128 characters long.You can also generate a password using Connection Manager. To do this, replace
--password=<password>with--generate-password.To view the password, select your cluster in the management console
, navigate to the Users tab, and click View password for the relevant user. This will open the page of the Yandex Lockbox secret containing the password. Viewing passwords requires thelockbox.payloadViewerrole. -
permissions: List of databases to which the user must have access. -
conn-limit: Maximum number of connections per user.
This command only configures the user’s basic settings.
The username may contain Latin letters, numbers, hyphens, and underscores but must begin with a letter (except the pg_ combination), number, or underscore. The name may be up to 63 characters long.
Such names as admin, repl, monitor, postgres, mdb_superuser, mdb_admin, mdb_monitor, and mdb_replication are reserved for Managed Service for PostgreSQL. You cannot create users with these names.
To configure the user’s DBMS settings, use the parameters described in User settings.
You can get the cluster name from the folder’s cluster list.
-
Open the current Terraform configuration file describing your infrastructure.
For more information about creating this file, see this guide.
For a complete list of editable user configuration fields in the Managed Service for PostgreSQL cluster, refer to the Terraform provider guides.
-
Add the
yandex_mdb_postgresql_userresource:resource "yandex_mdb_postgresql_user" "<username>" { cluster_id = "<cluster_ID>" name = "<username>" password = "<password>" grants = [ "<role1>","<role2>" ] login = <DB_login_permission> conn_limit = <maximum_number_of_connections> deletion_protection = <deletion_protection> settings = { <DB_settings> } permission { database_name = "<DB_name>" } }Where:
login: Permission to log in to the DB,trueorfalse.deletion_protection: User deletion protection,true,false, orunspecified(inherits the value from the cluster). The default value isunspecified.
The username may contain Latin letters, numbers, hyphens, and underscores but must begin with a letter (except the
pg_combination), number, or underscore. The name may be up to 63 characters long.Such names as
admin,repl,monitor,postgres,mdb_superuser,mdb_admin,mdb_monitor, andmdb_replicationare reserved for Managed Service for PostgreSQL. You cannot create users with these names.The password must be from 8 to 128 characters long.
You can also generate a password using Connection Manager. To do this, replace
password = "<password>"withgenerate_password = true.To view the password, select your cluster in the management console
, navigate to the Users tab, and click View password for the relevant user. This will open the page of the Yandex Lockbox secret containing the password. Viewing passwords requires thelockbox.payloadViewerrole. -
Validate your configuration.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Confirm updating the resources.
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
-
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Use the User.create method to execute the following request via cURL
:curl \ --request POST \ --header "Authorization: Bearer $IAM_TOKEN" \ --header "Content-Type: application/json" \ --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/users' \ --data '{ "userSpec": { "name": "<username>", "password": "<user_password>", "permissions": [ { "databaseName": "<DB_name>" } ], "connLimit": "<maximum_number_of_DB_connections>", "deletionProtection": <deletion_protection> } }'Where
userSpecare the settings for the new database user:-
name: Username.The username may contain Latin letters, numbers, hyphens, and underscores but must begin with a letter (except the
pg_combination), number, or underscore. The name may be up to 63 characters long.Such names as
admin,repl,monitor,postgres,mdb_superuser,mdb_admin,mdb_monitor, andmdb_replicationare reserved for Managed Service for PostgreSQL. You cannot create users with these names. -
password: Password. The password must be from 8 to 128 characters long.You can also generate a password using Connection Manager. To do this, specify
"generatePassword": trueinstead of"password": "<user_password>".To view the password, select the cluster you need in the management console
, go to the Users tab, and click View password in the new user row. This will open the page of the Yandex Lockbox secret containing the password. Viewing passwords requires thelockbox.payloadViewerrole. -
permissions.databaseName: Array of databases to which the user must have access. Each array item represents a single database. -
connLimit: Maximum number of database connections per user. -
deletionProtection: User deletion protection,true,false, orunspecified(inherits the value from the cluster). The default value isunspecified.
You can request the cluster ID with the list of clusters in the folder.
-
-
Check the server response to make sure your request was successful.
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapiBelow, we assume the repository contents are stored in the
~/cloudapi/directory. -
Use the ClusterService.Create call to execute the following request via gRPCurl
:grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/user_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "user_spec": { "name": "<username>", "password": "<user_password>", "permissions": [ { "database_name": "<DB_name>" } ], "conn_limit": "<maximum_number_of_DB_connections>", "deletion_protection": <deletion_protection> } }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.UserService.CreateWhere
user_specare the settings for the new database user:-
name: Username.The username may contain Latin letters, numbers, hyphens, and underscores but must begin with a letter (except the
pg_combination), number, or underscore. The name may be up to 63 characters long.Such names as
admin,repl,monitor,postgres,mdb_superuser,mdb_admin,mdb_monitor, andmdb_replicationare reserved for Managed Service for PostgreSQL. You cannot create users with these names. -
password: Password. The password must be from 8 to 128 characters long.You can also generate a password using Connection Manager. To do this, specify
"generate_password": trueinstead of"password": "<user_password>".To view the password, select the cluster you created in the management console
, go to the Users tab, and click View password in the new user row. This will open the page of the Yandex Lockbox secret containing the password. Viewing passwords requires thelockbox.payloadViewerrole. -
permissions.database_name: Array of databases to which the user must have access. Each array item represents a single database. -
conn_limit: Maximum number of database connections per user. -
deletion_protection: User deletion protection,true,false, orunspecified(inherits the value from the cluster). The default value isunspecified.
You can request the cluster ID with the list of clusters in the folder.
-
-
Check the server response to make sure your request was successful.
Note
Newly created users only get the CONNECT privilege for selected databases and cannot perform any operations on them. To give a user access to databases, assign them the required privileges or roles.
Changing a password
-
Navigate to the folder dashboard and select Managed Service for PostgreSQL.
-
Click the cluster name and select the Users tab.
-
Click
and select Change password. -
Select a method for setting a new password:
-
Enter manually: Set your own password. The password must be from 8 to 128 characters long.
-
Generate: Generate a password using Connection Manager.
-
-
Click Edit.
To view the new password, navigate to the cluster page, select the Users tab, and click View password for the relevant user. This will open the page of the Yandex Lockbox secret containing the password. The new password version is marked as Current.
Viewing passwords requires the
lockbox.payloadViewerrole.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
To change a user’s password, run this command:
yc managed-postgresql user update <username> \
--cluster-name=<cluster_name> \
--password=<new_password>
The password must be from 8 to 128 characters long.
You can also generate a new password using Connection Manager. To do this, replace --password=<new_password> with --generate-password.
To view the new password, select your cluster in the management console
Viewing passwords requires the lockbox.payloadViewer role.
You can get the cluster name from the folder’s cluster list.
-
Open the current Terraform configuration file describing your infrastructure.
For more information about creating this file, see this guide.
For a complete list of editable fields in the Managed Service for PostgreSQL cluster user configuration, see the Terraform provider documentation.
-
Locate the
yandex_mdb_postgresql_userresource for the required user. -
Update the
passwordfield:resource "yandex_mdb_postgresql_user" "<username>" { ... name = "<username>" password = "<new_password>" ... }The password must be from 8 to 128 characters long.
You can also generate a new password using Connection Manager. To do this, replace
password = "<new_password>"withgenerate_password = true.To view the new password, select your cluster in the management console
, navigate to the Users tab, and click View password for the relevant user. This will open the page of the Yandex Lockbox secret containing the password. The new password version is marked as Current.Note
If the current password has been automatically generated, you cannot regenerate it using Terraform due to provider limitations.
-
Validate your configuration.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Confirm updating the resources.
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
-
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Use the User.Update method to execute the following request via cURL
:Warning
The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the
updateMaskparameter as a single comma-separated string.curl \ --request PATCH \ --header "Authorization: Bearer $IAM_TOKEN" \ --header "Content-Type: application/json" \ --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/users/<username>' \ --data '{ "updateMask": "password", "password": "<new_password>" }'Where:
-
updateMask: Comma-separated list of settings you want to update.Here, we provide only one setting.
-
password: New password. The password must be from 8 to 128 characters long.You can also generate a password using Connection Manager. To do this, edit the contents of the
datafield:{ "updateMask": "generatePassword", "generatePassword": true }To view the new password, select your cluster in the management console
, navigate to the Users tab, and click View password for the relevant user. This will open the page of the Yandex Lockbox secret containing the password. The new password version is marked as Current.Viewing passwords requires the
lockbox.payloadViewerrole.
You can get the cluster ID from the folder’s cluster list, and the username from the cluster’s user list.
-
-
Check the server response to make sure your request was successful.
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapiBelow, we assume the repository contents are stored in the
~/cloudapi/directory. -
Use the ClusterService.Update call to execute the following request via gRPCurl
:Warning
The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the
update_maskparameter as an array ofpaths[]strings.Format for listing settings
"update_mask": { "paths": [ "<setting_1>", "<setting_2>", ... "<setting_N>" ] }grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/user_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "user_name": "<username>", "update_mask": { "paths": [ "password" ] }, "password": "<new_password>" }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.UserService.UpdateWhere:
-
update_mask: List of settings you want to update as an array of strings (paths[]).Here, we provide only one setting.
-
password: New password. The password must be from 8 to 128 characters long.You can also generate a password using Connection Manager. To do this, modify the
dparameter:{ "cluster_id": "<cluster_ID>", "user_name": "<username>", "update_mask": { "paths": [ "generate_password" ] }, "generate_password": true }To view the new password, select your cluster in the management console
, navigate to the Users tab, and click View password for the relevant user. This will open the page of the Yandex Lockbox secret containing the password. The new password version is marked as Current.Viewing passwords requires the
lockbox.payloadViewerrole.
You can get the cluster ID from the folder’s cluster list, and the username from the cluster’s user list.
-
-
Check the server response to make sure your request was successful.
Changing user settings
Note
These settings do not affect PostgreSQL privileges and roles, which are configured separately.
To learn how to assign privileges and roles to a user, see this article.
- Navigate to the folder dashboard and select Managed Service for PostgreSQL.
- Click the cluster name and select the Users tab.
- Click
and select Configure. - Configure user permissions for access to specific databases:
- To grant user access to the required databases:
- In the Database field, click
next to the drop-down list. - Select a database from the drop-down list.
- Repeat these two steps to select all required databases.
- In the Database field, click
- To revoke user access to a specific database, click
next to its name.
- To grant user access to the required databases:
- Click DBMS settings to change the user’s maximum connections (Conn limit), cluster login permissions (Login), or other PostgreSQL settings.
- Click Save.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
You can change user settings from the command line interface:
-
To configure user access to specific databases, run this command with a list of database names specified in the
--permissionsparameter:yc managed-postgresql user update <username> \ --cluster-name=<cluster_name> \ --permissions=<database_list>Where:
cluster-name: Cluster name.permissions: List of databases to which the user must have access.
You can get the cluster name from the folder’s cluster list.
This command grants the user access to the listed databases.
To revoke user access to a specific database, remove its name from the list and run the command with the updated list.
-
To change PostgreSQL user settings, run this command with the relevant arguments:
yc managed-postgresql user update <username> \ --cluster-name=<cluster_name> \ --<setting_1>=<value_1> \ --<setting_2>=<value_2> \ --<setting_3>=<list_of_values> \ ...To change a user’s connection limit, use the
--conn-limitparameter.You can get the cluster name from the folder’s cluster list.
-
Open the current Terraform configuration file describing your infrastructure.
For more information about creating this file, see this guide.
For a complete list of editable user configuration fields in the Managed Service for PostgreSQL cluster, refer to the Terraform provider guides.
-
To configure user permissions for access to specific databases:
-
Locate the
yandex_mdb_postgresql_userresource for the required user. -
Add
permissionsections with the relevant database names:resource "yandex_mdb_postgresql_user" "<username>" { ... name = "<username>" permission { database_name = "<DB_name>" } permission { database_name = "<DB_name>" } ... }
-
-
To revoke user access to a specific database, delete the
permissionsection with its name from the configuration file. -
To change PostgreSQL user settings, specify the relevant arguments in the
settingssection:resource "yandex_mdb_postgresql_user" "<username>" { ... name = "<username>" settings = { <DB_settings> } ... } -
Make sure the settings are correct.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Confirm updating the resources.
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
-
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Use the User.Update method to execute the following request via cURL
:Warning
The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the
updateMaskparameter as a single comma-separated string.curl \ --request PATCH \ --header "Authorization: Bearer $IAM_TOKEN" \ --header "Content-Type: application/json" \ --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/users/<username>' \ --data '{ "updateMask": "settings", "settings": { <settings> } }'Where:
-
updateMask: Comma-separated list of settings you want to update.Here, we provide only one setting.
-
settings: New settings. For the list of available settings, refer to the method description and User-level settings.
You can get the cluster ID from the folder’s cluster list, and the username from the cluster’s user list.
-
-
Check the server response to make sure your request was successful.
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapiBelow, we assume the repository contents are stored in the
~/cloudapi/directory. -
Use the ClusterService.Update call to execute the following request via gRPCurl
:Warning
The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the
update_maskparameter as an array ofpaths[]strings.Format for listing settings
"update_mask": { "paths": [ "<setting_1>", "<setting_2>", ... "<setting_N>" ] }grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/user_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "user_name": "<username>", "update_mask": { "paths": [ "settings" ] }, "settings": { <settings> } }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.UserService.UpdateWhere:
-
update_mask: List of settings you want to update as an array of strings (paths[]).Here, we provide only one setting.
-
settings: New settings. For the list of available settings, refer to the method description and User-level settings.
You can get the cluster ID from the folder’s cluster list, and the username from the cluster’s user list.
-
-
Check the server response to make sure your request was successful.
Configuring deletion protection
- Navigate to the folder dashboard
and select Managed Service for PostgreSQL. - Click the cluster name and select the Users tab.
- Click
and select Configure. - Configure user deletion protection by selecting the relevant value in the Deletion protection field.
- Click Save.
-
Open the current Terraform configuration file describing your infrastructure.
-
Locate the
yandex_mdb_postgresql_userresource for the required user. -
Add the
deletion_protectionparameter. The possible values aretrue,false, orunspecified(inherits the value from the cluster). The default value isunspecified.resource "yandex_mdb_postgresql_user" "<username>" { ... deletion_protection = <deletion_protection> ... } -
Make sure the settings are correct.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Confirm updating the resources.
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
-
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Use the User.Update method to execute the following request via cURL
:Warning
The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the
updateMaskparameter as a single comma-separated string.curl \ --request PATCH \ --header "Authorization: Bearer $IAM_TOKEN" \ --header "Content-Type: application/json" \ --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/users/<username>' \ --data '{ "updateMask": "deletionProtection", "deletionProtection": <deletion_protection> } }'Where:
-
updateMask: Comma-separated list of settings you want to update.Here, we provide only one setting.
-
deletionProtection: User deletion protection,true,false, orunspecified(inherits the value from the cluster). The default value isunspecified.
You can get the cluster ID from the folder’s cluster list, and the username from the cluster’s user list.
-
-
Check the server response to make sure your request was successful.
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapiBelow, we assume the repository contents are stored in the
~/cloudapi/directory. -
Use the ClusterService.Update call to execute the following request via gRPCurl
:Warning
The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the
update_maskparameter as an array ofpaths[]strings.Format for listing settings
"update_mask": { "paths": [ "<setting_1>", "<setting_2>", ... "<setting_N>" ] }grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/user_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "user_name": "<username>", "update_mask": { "paths": [ "deletion_protection" ] }, "deletion_protection": <deletion_protection> }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.UserService.UpdateWhere:
-
update_mask: List of settings you want to update as an array of strings (paths[]).Here, we provide only one setting.
-
deletion_protection: User deletion protection,true,false, orunspecified(inherits the value from the cluster). The default value isunspecified.
You can get the cluster ID from the folder’s cluster list, and the username from the cluster’s user list.
-
-
Check the server response to make sure your request was successful.
Warning
Deletion protection only applies to individual users. Deleting a cluster will permanently remove all its users, including those with deletion protection.
Deleting a user
A user account can have deletion protection enabled. To delete such a user, disable the protection first.
To delete a user:
- Navigate to the folder dashboard and select Managed Service for PostgreSQL.
- Click the cluster name and select the Users tab.
- Click
and select Delete. - Confirm the deletion.
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
By default, the CLI uses the folder specified when creating the profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also set a different folder for any specific command using the --folder-name or --folder-id parameter.
To delete a user, run this command:
yc managed-postgresql user delete <username> \
--cluster-name <cluster_name>
You can get the cluster name from the folder’s cluster list.
To delete a user:
-
Open the current Terraform configuration file describing your infrastructure.
For more information about creating this file, see this guide.
For a complete list of configurable Managed Service for PostgreSQL cluster fields, refer to the Terraform provider guides.
-
Delete the
yandex_mdb_postgresql_userresource describing the target user. -
Validate your configuration.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Confirm updating the resources.
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
-
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Use the User.Delete method to execute the following request via cURL
:curl \ --request DELETE \ --header "Authorization: Bearer $IAM_TOKEN" \ --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/users/<username>'You can get the cluster ID from the folder’s cluster list, and the username from the cluster’s user list.
-
Check the server response to make sure your request was successful.
-
Get an IAM token for API authentication and place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Clone the cloudapi
repository:cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapiBelow, we assume the repository contents are stored in the
~/cloudapi/directory. -
Use the ClusterService.Delete call to execute the following request via gRPCurl
:grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/user_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "user_name": "<username>" }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.UserService.DeleteYou can get the cluster ID from the folder’s cluster list, and the username from the cluster’s user list.
-
Check the server response to make sure your request was successful.
Examples
Add a user with read-only permissions
Alert
Do not use this example if a user is created using Terraform: subsequent changes made via Terraform may cancel the user's privileges granted through SQL.
To add a new user (user2) to an existing cluster with read-only access to the db1 database:
-
Create a user named
user2. Select the databases that the user should have access to. -
Connect to the
db1database under the database owner account. -
Grant
user2the required permissions.Examples:
-
Grant access only to the
Productstable in the defaultpublicschema:GRANT SELECT ON public.Products TO user2; -
Grant access to objects in
myschema:GRANT USAGE ON SCHEMA myschema TO user2; -
Grant access to all tables and sequences in
myschema:GRANT SELECT ON ALL TABLES IN SCHEMA myschema TO user2; GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA myschema to user2; -
Allow calling
my_functioninmyschema:GRANT EXECUTE ON FUNCTION myschema.my_function TO user2; -
Change the default privileges for tables and sequences in
myschema:ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT SELECT ON TABLES TO user2; ALTER DEFAULT PRIVILEGES IN SCHEMA myschema GRANT USAGE, SELECT ON SEQUENCES TO user2;The
ALTER DEFAULT PRIVILEGEScommands allow you to change access permissions for new objects you will create later (in this case,myschematables and sequences) but do not affect permissions granted for existing objects.To update privileges for existing objects, use the
GRANTandREVOKEcommands.
-
You can grant user privileges via Terraform only in a cluster with public hosts.
User privileges are granted via Terraform using a third-party provider, Terraform Provider for PostgreSQL
Note
Terraform Provider for PostgreSQL is not part of Managed Service for PostgreSQL. It is not covered by Yandex Cloud support and its usage is not governed by the Yandex Managed Service for PostgreSQL Terms of Use
For more information about granting privileges, see Granting a privilege to a user.
Let's say you have a cluster named mypg with user1 as the owner. To add a new user (user2) to this cluster with read-only access to db1 tables with the public schema:
-
Add the
postgresqlprovider to therequired_providerssection in the provider configuration file:terraform { required_providers { ... postgresql = { source = "cyrilgdn/postgresql" } ... } } -
Open the Terraform configuration file with the infrastructure plan.
-
Add the
yandex_mdb_postgresql_userresource:resource "yandex_mdb_postgresql_user" "user2" { cluster_id = yandex_mdb_postgresql_cluster.mypg.id name = "user2" password = "user2user2" permission { database_name = yandex_mdb_postgresql_database.db1.name } } -
Add the
postgresqlprovider and configure its access permissions todb1:provider "postgresql" { host = yandex_mdb_postgresql_cluster.mypg.host[0].fqdn port = 6432 database = yandex_mdb_postgresql_database.db1.name username = yandex_mdb_postgresql_user.user1.name password = yandex_mdb_postgresql_user.user1.password } -
Add the
postgresql_grantresource with the following attributes:resource "postgresql_grant" "readonly_tables" { database = yandex_mdb_postgresql_database.db1.name role = yandex_mdb_postgresql_user.user2.name object_type = "table" privileges = ["SELECT"] schema = "public" } -
Initialize Terraform once again:
terraform init -
Make sure the settings are correct.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Confirm updating the resources.
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
-
Revoking permissions
-
Connect to the
db1database under the database owner account. -
Revoke the relevant access permissions from
user2.Examples:
-
Revoke all privileges for tables in the
myschemaschema:REVOKE ALL PRIVILEGES ON ALL TABLES IN SCHEMA myschema FROM user2; -
Revoke access for the
Productstable in the defaultpublicschema:REVOKE SELECT ON public.Products FROM user2; -
Revoke access to all tables in
myschema:REVOKE SELECT ON ALL TABLES IN SCHEMA myschema FROM user2; -
Revoke access for objects in the
myschemaschema:REVOKE USAGE ON SCHEMA myschema FROM user2;
-
-
Open the Terraform configuration file you used to grant privileges.
-
In the
postgresql_grantsection, remove the privilege you want to revoke from theprivilegesparameter.To revoke all privileges, leave the
privilegesarray empty or completely remove thepostgresql_grantresource.resource "postgresql_grant" "readonly_tables" { database = yandex_mdb_postgresql_database.db1.name role = yandex_mdb_postgresql_user.user2.name object_type = "table" privileges = [] schema = "public" } -
Make sure the settings are correct.
-
In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
-
Run this command:
terraform validateTerraform will show any errors found in your configuration files.
-
-
Confirm updating the resources.
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
-