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 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>" -
Call the Database.List method, for instance, 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 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. -
Call the DatabaseService.List method, for instance, 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 get the cluster ID from the folder’s cluster list.
-
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 relevant cluster’s 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 maximum name length is 63 characters. The names
postgres,template0, andtemplate1are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using these names. -
Owner
-
Deletion protection
Possible values:
- Same as cluster
- Enabled
- Disabled
-
Template: The name of 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 supports multiple locales. The choice of locale affects the following:
- Sort order in queries using the
ORDER BYclause or standard text comparison operators. upper,lower,initcap, andto_charfunctions.- Pattern-matching operators, e.g.,
LIKE,ILIKE,SIMILAR TO, and regular expressions. - Support of indexes with the
LIKEoperator.
The default locale is
C. When using theCencoding for text data with non-Latin, e.g., Cyrillic, characters, you may encounter errors in data sorting and incorrect pattern-matching results. If this locale causes incorrect data handling in your database, select a different one from the list. However, note that using a non-standard locale may reduce database query performance.To learn more about locale settings, see the this PostgreSQL article
.Once you create a database, you cannot change its locale settings. However, you can set a collation for columns when creating or altering individual tables. For more information, see the relevant PostgreSQL guides
. - Sort order in queries using 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 necessary, create a new user.
-
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 maximum name length is 63 characters. The names
postgres,template0, andtemplate1are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using these names.You can get the cluster name from the folder’s cluster list.
Managed Service for PostgreSQL will launch the database creation process.
-
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 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: Name of the user who will own the database, defined in theyandex_mdb_postgresql_userresource.deletion_protection: Database deletion protection. The possible values aretrue,false, orunspecified(inherits the value from the cluster). The default value isunspecified.
The database name may contain Latin letters, numbers, underscores, and hyphens. The maximum name length is 63 characters. The names
postgres,template0, andtemplate1are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using these names. -
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 resource changes.
-
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 place it in an environment variable:
export IAM_TOKEN="<IAM_token>" -
Call the Database.Create method, for instance, 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 and having the the following structure:-
name: Database name.The database name may contain Latin letters, numbers, underscores, and hyphens. The maximum name length is 63 characters. The names
postgres,template0, andtemplate1are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using 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.
Use a name from the list of supported PostgreSQL extensions and utilities.
-
deletionProtection: Database deletion protection. The possible values aretrue,false, orunspecified(inherits the value from the cluster). The default value isunspecified.
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. -
Call the DatabaseService.Create method, for instance, 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 and having the the following structure:-
name: Database name.The database name may contain Latin letters, numbers, underscores, and hyphens. The maximum name length is 63 characters. The names
postgres,template0, andtemplate1are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using 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: Database deletion protection. The possible values aretrue,false, orunspecified(inherits the value from the cluster). The default value isunspecified.
You can get the cluster ID from the folder’s cluster list.
-
-
Check the server response to make sure your request was successful.
Renaming a database
-
Open the current Terraform configuration file describing your infrastructure.
To learn how to create this file, see Creating clusters.
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. -
Update the
namefield:resource "yandex_mdb_postgresql_database" "<database_name>" { ... name = "<new_database_name>" ... }The database name may contain Latin letters, numbers, underscores, and hyphens. The maximum name length is 63 characters. The names
postgres,template0, andtemplate1are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using these names. -
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 resource changes.
-
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>" -
Call the Database.Update method, for instance, 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 maximum name length is 63 characters. The names
postgres,template0, andtemplate1are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using these names.
You can get the cluster ID from the folder’s cluster list, and the database name from the cluster’s database 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. -
Call the DatabaseService.Update method, for instance, via the following gRPCurl
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
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 maximum name length is 63 characters. The names
postgres,template0, andtemplate1are reserved for internal use by Managed Service for PostgreSQL. You cannot create databases using these names.
You can get the cluster ID from the folder’s cluster list, and the database name from the list of cluster databases.
-
-
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.
- Find the database you need in the list, click
in its row, then 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_protectionattribute. 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> ... } -
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 resource changes.
-
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>" -
Call the Database.Update method, for instance, 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: Database 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 database name from the list of cluster databases.
-
-
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. -
Call the DatabaseService.Update method, for instance, via the following gRPCurl
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
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: Database 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 database name from the list of cluster databases.
-
-
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.
- Find 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 from the folder’s cluster list.
To delete a database:
-
Open the current Terraform configuration file describing your infrastructure.
To learn how to create this file, see Creating clusters.
For a complete list of configurable Managed Service for PostgreSQL cluster database settings, refer to the Terraform provider guides.
-
Delete the
yandex_mdb_postgresql_databaseresource that has the name of your target database. -
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 resource changes.
-
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>" -
Call the Database.Delete method, for instance, 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 from the folder’s cluster list, and the database name from the list of cluster databases.
-
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. -
Call the DatabaseService.Delete method, for instance, 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 from the folder’s cluster list, and the database name from the list of cluster databases.
-
Check the server response to make sure your request was successful.
Warning
Wait for the deletion operation to fully complete before creating a new database with the same name. Otherwise, the original database will be restored. You can check the operation status in the list of cluster operations.