Managing databases in Managed Service for PostgreSQL
You can add, rename, and drop databases, as well as view their details.
Warning
You can't manage databases using SQL commands.
Getting a list of cluster databases
- Go to Managed Service for PostgreSQL.
- Click the name of your cluster and open the Databases 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 databases, run this command:
yc managed-postgresql database list --cluster-name=<cluster_name>
You can request the cluster name with the list of clusters in the folder.
-
Get an IAM token for API authentication and put it into an environment variable:
export IAM_TOKEN="<IAM_token>" -
Call the Database.List method, e.g., via the following cURL
request:curl \ --request GET \ --header "Authorization: Bearer $IAM_TOKEN" \ --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/databases'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 put it into 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. -
Call the DatabaseService.List method, e.g., via the following gRPCurl
request:grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/database_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>" }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.DatabaseService.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 database
Note
You can create a maximum of 1000 databases in each cluster.
-
Go to Managed Service for PostgreSQL.
-
Click the cluster name.
-
If necessary, create a new user account for database owner.
-
Select the Databases tab.
-
Click
Create database. -
Specify database settings:
-
Name
The database name may contain Latin letters, numbers, underscores, and hyphens. The name may be up to 63 characters long. The names
postgres,template0, andtemplate1are reserved for Managed Service for PostgreSQL. You cannot create databases with these names. -
Owner
-
Deletion protection
The possible values are:
- Same as cluster
- Enabled
- Disabled
-
Template: The name an existing database to use as a template for the new database’s schema. This is an optional setting. Creating the new database will close all active connections to the template database.
For more information, see this PostgreSQL guide
. -
Locales for collation and character set.
PostgreSQL uses locales to support various language standards. The locale you choose affects:
- Sort order in the queries that use the
ORDER BYoperator or standard text data matching operators. - The functions
upper,lower,initcap, and theto_charfamily of functions. - Pattern matching operators (
LIKE,ILIKE,SIMILAR TO, regular expressions). - Support of indexes with the
LIKEoperator.
By default, the
Clocale is used. if you use theCencoding for text data containing non-Latin (for example, Cyrillic) characters, errors might occur in the data sort order and data display in the case of pattern search. If this locale is not suitable for valid processing of tables in your database, select another encoding from the list. However, please keep in mind that a non-standard locale might decrease the database query processing rate.For more information about locale settings, see the PostgreSQL documentation
.You cannot change locale settings after you create a database. However, you can set the sorting locale for columns when creating and modifying individual tables. Learn more in the PostgreSQL documentation
. - Sort order in the queries that use the
-
-
Click Create.
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 database in a cluster:
-
See the description of the CLI command for creating a database:
yc managed-postgresql database create --help -
Request a list of cluster users to select the owner for the new database:
yc managed-postgresql user list --cluster-name=<cluster_name>If the required user is not in the list, create it.
-
Run the database creation command. Optionally, specify the template and your preferred collation and character set locales. The default locale settings are
LC_COLLATE=CandLC_CTYPE=C:yc managed-postgresql database create <DB_name> \ --cluster-name=<cluster_name> \ --owner=<DB_owner_name> \ --lc-collate=<collation_locale> \ --lc-type=<character_set_locale> \ --template-db=<DB_template_name>The database name may contain Latin letters, numbers, underscores, and hyphens. The name may be up to 63 characters long. The names
postgres,template0, andtemplate1are reserved for Managed Service for PostgreSQL. You cannot create databases with these names.You can request the cluster name with the list of clusters in the folder.
Managed Service for PostgreSQL will start creating the database.
-
Open the current Terraform configuration file describing your infrastructure.
For information on how to create this file, see Creating a cluster.
For a complete list of configurable Managed Service for PostgreSQL cluster database settings, refer to the Terraform provider guides.
-
Add the
yandex_mdb_postgresql_databaseresource: Optionally, specify the template and your preferred collation and character set locales. The default locale settings areLC_COLLATE=CandLC_CTYPE=C:resource "yandex_mdb_postgresql_database" "<DB_name>" { cluster_id = "<cluster_ID>" name = "<DB_name>" owner = "<DB_owner_name>" lc_collate = "<collation_locale>" lc_type = "<character_set_locale>" template_db = "<DB_template_name>" deletion_protection = <deletion_protection> }Where:
owner: Username of the owner that must be specified in theyandex_mdb_postgresql_userresource.deletion_protection: DB deletion protection,true,false, orunspecified(inherits the value from the cluster). The default value isunspecified.
The database name may contain Latin letters, numbers, underscores, and hyphens. The name may be up to 63 characters long. The names
postgres,template0, andtemplate1are reserved for Managed Service for PostgreSQL. You cannot create databases with these names. -
Check if 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.
-
-
Warning
Changing the owner of the existing database by editing the owner setting will trigger the database recreation, resulting in data loss.
-
Get an IAM token for API authentication and put it into an environment variable:
export IAM_TOKEN="<IAM_token>" -
Call the Database.Create method, e.g., via the following cURL
request: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>/databases' \ --data '{ "databaseSpec": { "name": "<DB_name>", "owner": "<DB_owner_name>", "lcCollate": "<collation_locale>", "lcCtype": "<character_set_locale>", "extensions": [ { "name": "<extension_name>" } ], "deletionProtection": <deletion_protection> } }'Where
databaseSpecis the object containing the settings for the new database, with the following structure:-
name: Database name.The database name may contain Latin letters, numbers, underscores, and hyphens. The name may be up to 63 characters long. The names
postgres,template0, andtemplate1are reserved for Managed Service for PostgreSQL. You cannot create databases with these names. -
owner: Database owner. -
lcCollate: Collation locale. The default value isC. -
lcCtype: Character set locale. The default value isC. -
extensions: Array of database extensions. Each array element contains the configuration for a single extension and has the following structure:extensions.name: Extension name.
Give a name according to the list of supported PostgreSQL extensions and utilities.
-
deletionProtection: DB 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 put it into 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. -
Call the DatabaseService.Create method, e.g., via the following gRPCurl
request:grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/database_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "database_spec": { "name": "<DB_name>", "owner": "<DB_owner_name>", "lc_collate": "<collation_locale>", "lc_ctype": "<character_set_locale>", "extensions": [ { "name": "<extension_name>" } ], "deletion_protection": <deletion_protection> } }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.DatabaseService.CreateWhere
databaseSpecis the object containing the settings for the new database, with the following structure:-
name: Database name.The database name may contain Latin letters, numbers, underscores, and hyphens. The name may be up to 63 characters long. The names
postgres,template0, andtemplate1are reserved for Managed Service for PostgreSQL. You cannot create databases with these names. -
owner: Database owner. -
lc_collate: Collation locale. The default value isC. -
lc_ctype: Character set locale. The default value isC. -
extensions: Array of database extensions. Each array element contains the configuration for a single extension and has the following structure:extensions.name: Extension name.
Use a name from the list of supported PostgreSQL extensions and utilities.
-
deletion_protection: DB 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.
Renaming a database
-
Open the current Terraform configuration file describing your infrastructure.
For information on how to create this file, see Creating a cluster.
For a complete list of configurable Managed Service for PostgreSQL cluster database settings, refer to the Terraform provider guides.
-
Find the
yandex_mdb_postgresql_databaseresource describing your target database. -
Change the
namefield value:resource "yandex_mdb_postgresql_database" "<database_name>" { ... name = "<new_database_name>" ... }The database name may contain Latin letters, numbers, underscores, and hyphens. The name may be up to 63 characters long. The names
postgres,template0, andtemplate1are reserved for Managed Service for PostgreSQL. You cannot create databases with these names. -
Check if 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 put it into an environment variable:
export IAM_TOKEN="<IAM_token>" -
Call the Database.Update method, e.g., via the following cURL
request: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>/databases/<previous_DB_name>' \ --data '{ "updateMask": "newDatabaseName", "newDatabaseName": "<new_DB_name>" }'Where:
-
updateMask: Comma-separated list of settings you want to update.Here, we provide only one setting.
-
newDatabaseName: New database name.The database name may contain Latin letters, numbers, underscores, and hyphens. The name may be up to 63 characters long. The names
postgres,template0, andtemplate1are reserved for Managed Service for PostgreSQL. You cannot create databases with these names.
You can get the cluster ID with the list of clusters in the folder, and the database name, with the list of databases in the cluster.
-
-
Check the server response to make sure your request was successful.
-
Get an IAM token for API authentication and put it into 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 DatabaseService.Update call to execute the following request via gRPCurl
(or another tool of your choice):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/database_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "database_name": "<previous_DB_name>", "update_mask": { "paths": [ "new_database_name" ] }, "new_database_name": "<new_DB_name>" }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.DatabaseService.UpdateWhere:
-
update_mask: List of settings you want to update as an array of strings (paths[]).Here, we provide only one setting.
-
new_database_name: New database name.The database name may contain Latin letters, numbers, underscores, and hyphens. The name may be up to 63 characters long. The names
postgres,template0, andtemplate1are reserved for Managed Service for PostgreSQL. You cannot create databases with these names.
You can get the cluster ID with the list of clusters in the folder, and the database name, with the list of databases in the cluster.
-
-
Check the server response to make sure your request was successful.
Configuring deletion protection
- Go to Managed Service for PostgreSQL.
- Click the name of your cluster and open the Databases tab.
- Click
in the relevant database row and select Configure. - Select your preferred option in the Deletion protection field.
- Click Save.
-
Open the current Terraform configuration file describing your infrastructure.
-
Find the
yandex_mdb_postgresql_databaseresource describing your target database. -
Add the
deletion_protectionparameter. The possible values aretrue,false, orunspecified(inherits the value from the cluster). The default value isunspecified.resource "yandex_mdb_postgresql_database" "<DB_name>" { ... deletion_protection = <deletion_protection> ... } -
Check if 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 put it into an environment variable:
export IAM_TOKEN="<IAM_token>" -
Call the Database.Update method, e.g., via the following cURL
request: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>/databases/<DB_name>' \ --data '{ "updateMask": "deletionProtection", "deletionProtection": <deletion_protection> }'Where:
-
updateMask: Comma-separated list of settings you want to update.Here, we provide only one setting.
-
deletionProtection: DB deletion protection,true,false, orunspecified(inherits the value from the cluster). The default value isunspecified.
You can get the cluster ID with the list of clusters in the folder, and the database name, with the list of databases in the cluster.
-
-
View the server response to make sure your request was successful.
-
Get an IAM token for API authentication and put it into 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 DatabaseService.Update call to execute the following request via gRPCurl
(or another tool of your choice):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/database_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "database_name": "<DB_name>", "update_mask": { "paths": [ "deletion_protection" ] }, "deletion_protection": <deletion_protection> }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.DatabaseService.UpdateWhere:
-
update_mask: List of settings you want to update as an array of strings (paths[]).Here, we provide only one setting.
-
deletion_protection: DB deletion protection,true,false, orunspecified(inherits the value from the cluster). The default value isunspecified.
You can get the cluster ID with the list of clusters in the folder, and the database name, with the list of databases in the cluster.
-
-
Check the server response to make sure your request was successful.
Warning
Deletion protection only applies to individual databases. Deleting a cluster will permanently remove all its databases, including those with deletion protection.
Deleting a database
A database can have deletion protection enabled. To delete it, first disable deletion protection.
To delete a database:
- Go to Managed Service for PostgreSQL.
- Click the name of your cluster and open the Databases tab.
- Select the database you need in the list, click
in its row, select Delete, then 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 database, run this command:
yc managed-postgresql database delete <DB_name> \
--cluster-name <cluster_name>
You can get the cluster name with the list of clusters in the folder.
To delete a database:
-
Open the current Terraform configuration file describing your infrastructure.
For information on how to create this file, see Creating a cluster.
For a complete list of configurable Managed Service for PostgreSQL cluster database settings, refer to the Terraform provider guides.
-
Remove the
yandex_mdb_postgresql_databaseresource with the name of the database you are deleting. -
Check if 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 put it into an environment variable:
export IAM_TOKEN="<IAM_token>" -
Call the Database.Delete method, e.g., via the following cURL
request:curl \ --request DELETE \ --header "Authorization: Bearer $IAM_TOKEN" \ --url 'https://mdb.api.cloud.yandex.net/managed-postgresql/v1/clusters/<cluster_ID>/databases/<DB_name>'You can get the cluster ID with the list of clusters in the folder, and the database name, with the list of databases in the cluster.
-
View the server response to make sure your request was successful.
-
Get an IAM token for API authentication and put it into 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. -
Call the DatabaseService.Delete method, e.g., via the following gRPCurl
request:grpcurl \ -format json \ -import-path ~/cloudapi/ \ -import-path ~/cloudapi/third_party/googleapis/ \ -proto ~/cloudapi/yandex/cloud/mdb/postgresql/v1/database_service.proto \ -rpc-header "Authorization: Bearer $IAM_TOKEN" \ -d '{ "cluster_id": "<cluster_ID>", "database_name": "<DB_name>" }' \ mdb.api.cloud.yandex.net:443 \ yandex.cloud.mdb.postgresql.v1.DatabaseService.DeleteYou can get the cluster ID with the list of clusters in the folder, and the database name, with the list of databases in the cluster.
-
Check the server response to make sure your request was successful.
Warning
Before creating a new database with the same name, wait for the delete operation to complete. Otherwise, the original database will be restored. You can get the operation status with the list of cluster operations.