Managing databases in Managed Service for PostgreSQL
You can add, rename, and remove databases, as well as view information about them.
Warning
You can't manage databases using SQL commands.
Getting a list of cluster databases
- Navigate to the folder dashboard
and select Managed Service for PostgreSQL. - Click the cluster name 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 databases in a cluster, run the 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 the environment variable:
export IAM_TOKEN="<IAM_token>" -
Use the Database.List method and send the following request, e.g., via cURL
: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.
-
View the server response to make sure the request was successful.
-
Get an IAM token for API authentication and put it into the 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.List call and send the following request, e.g., via gRPCurl
: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.
-
View the server response to make sure the request was successful.
Creating a database
Note
You can create a maximum of 1000 databases in each cluster.
-
Navigate to the folder dashboard
and select Managed Service for PostgreSQL. -
Click the cluster name.
-
If the new database does not have an owner among its current users, add such a user.
-
Select the Databases tab.
-
Click
Create database. -
Specify the 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
-
(Optional) Template: The name of one of the existing databases from which the data schema needs to be copied. All connections to the template database will be closed while the new database is being created.
For more information, see the PostgreSQL documentation
. -
Collation and character set locales.
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:
-
View a description of the CLI create database command:
yc managed-postgresql database create --help -
Request a list of cluster users to select the owner of 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 create database command. If necessary, specify the required collation and character set locales (the default ones are
LC_COLLATE=CandLC_CTYPE=C) and the template:yc managed-postgresql database create <DB_name> \ --cluster-name=<cluster_name> \ --owner=<database_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 runs the create database operation.
-
Open the current Terraform configuration file that defines your infrastructure.
For more information about creating this file, see Creating clusters.
For a complete list of editable fields in the Managed Service for PostgreSQL cluster database configuration, see the Terraform provider documentation.
-
Add the
yandex_mdb_postgresql_databaseresource: If necessary, specify the required collation and character set locales (the default ones areLC_COLLATE=CandLC_CTYPE=C) and the template:resource "yandex_mdb_postgresql_database" "<DB_name>" { cluster_id = "<cluster_ID>" name = "<DB_name>" owner = "<database_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. -
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.
-
-
Warning
After you create a DB, do not change its owner in the owner parameter, as this will recreate the DB and its data will be lost.
-
Get an IAM token for API authentication and put it into the environment variable:
export IAM_TOKEN="<IAM_token>" -
Use the Database.Create method and send the following request, e.g., 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>/databases' \ --data '{ "databaseSpec": { "name": "<DB_name>", "owner": "<database_owner_name>", "lcCollate": "<collation_locale>", "lcCtype": "<character_set_locale>", "extensions": [ { "name": "<extension_name>" } ], "deletionProtection": <deletion_protection> } }'Where
databaseSpecis the object containing the new DB settings: Its structure is as follows:-
name: DB 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: DB owner username. -
lcCollate: Collation locale. The default value isC. -
lcCtype: Character set locale. The default value isC. -
extensions: Array of DB extensions. One array element contains settings 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.
-
-
View the server response to make sure the request was successful.
-
Get an IAM token for API authentication and put it into the 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.Create call and send the following request, e.g., via gRPCurl
: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": "<database_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 new DB settings: Its structure is as follows:-
name: DB 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: DB owner username. -
lc_collate: Collation locale. The default value isC. -
lc_ctype: Character set locale. The default value isC. -
extensions: Array of DB extensions. One array element contains settings 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.
-
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.
-
-
View the server response to make sure the request was successful.
Renaming a database
-
Open the current Terraform configuration file that defines your infrastructure.
For more information about creating this file, see Creating clusters.
For a complete list of editable fields in the Managed Service for PostgreSQL cluster database configuration, see the Terraform provider documentation.
-
Find the
yandex_mdb_postgresql_databaseresource of the database you need. -
Change the value of 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 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. -
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 put it into the environment variable:
export IAM_TOKEN="<IAM_token>" -
Use the Database.Update method and send the following request, e.g., 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>/databases/<previous_DB_name>' \ --data '{ "updateMask": "newDatabaseName", "newDatabaseName": "<new_DB_name>" }'Where:
-
updateMask: List of parameters to update as a single string, separated by commas.In this case, only one parameter is provided.
-
newDatabaseName: New DB 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 ID with the list of clusters in the folder, and the DB name, with the list of DBs in the cluster.
-
-
View the server response to make sure the request was successful.
-
Get an IAM token for API authentication and put it into the 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 and send the following request, e.g., 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/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 parameters to update as an array ofpaths[]strings.Only one parameter is provided in this case.
-
new_database_name: New DB 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 ID with the list of clusters in the folder, and the DB name, with the list of DBs in the cluster.
-
-
View the server response to make sure the request was successful.
Configuring deletion protection
- Navigate to the folder dashboard
and select Managed Service for PostgreSQL. - Click the cluster name and open the Databases tab.
- Click
in the required DB row and select Configure. - Select the appropriate value in the Deletion protection field.
- Click Save.
-
Open the current Terraform configuration file that defines your infrastructure.
-
Find the
yandex_mdb_postgresql_databaseresource of the DB you need. -
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> ... } -
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 put it into the environment variable:
export IAM_TOKEN="<IAM_token>" -
Use the Database.Update method and send the following request, e.g., 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>/databases/<DB_name>' \ --data '{ "updateMask": "deletionProtection", "deletionProtection": <deletion_protection> }'Where:
-
updateMask: List of parameters to update as a single string, separated by commas.Only one parameter is provided in this case.
-
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, and the DB name, with the list of DBs in the cluster.
-
-
View the server response to make sure the request was successful.
-
Get an IAM token for API authentication and put it into the 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 and send the following request, e.g., 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/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 parameters to update as an array ofpaths[]strings.Only one parameter is provided in this case.
-
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, and the DB name, with the list of DBs in the cluster.
-
-
View the server response to make sure the request was successful.
Warning
Deletion protection only applies at specific DB level. Deleting a cluster will delete all DBs, including those protected from deletion.
Deleting a database
A DB can be protected against deletion. To delete such a DB, disable the protection first.
To delete a database:
- Navigate to the folder dashboard
and select Managed Service for PostgreSQL. - Click the cluster name and open the Databases tab.
- Click
in the required DB row, select Delete, and 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 the command:
yc managed-postgresql database delete <DB_name> \
--cluster-name <cluster_name>
You can request the cluster name with the list of clusters in the folder.
To delete a database:
-
Open the current Terraform configuration file that defines your infrastructure.
For more information about creating this file, see Creating clusters.
For a complete list of editable fields in the Managed Service for PostgreSQL cluster database configuration, see the Terraform provider documentation.
-
Delete the
yandex_mdb_postgresql_databaseresource with the name of the database you want to delete. -
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 put it into the environment variable:
export IAM_TOKEN="<IAM_token>" -
Use the Database.Delete method and send the following request, e.g., via cURL
: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 request the cluster ID with the list of clusters in the folder, and the DB name, with the list of DBs in the cluster.
-
View the server response to make sure the request was successful.
-
Get an IAM token for API authentication and put it into the 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.Delete call and send the following request, e.g., via gRPCurl
: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 request the cluster ID with the list of clusters in the folder, and the DB name, with the list of DBs in the cluster.
-
View the server response to make sure the request was successful.
Warning
Before creating a new database with the same name, wait for the delete operation to complete, otherwise the database being deleted will be restored. Operation status can be obtained with a list of cluster operations.