Setting up a Yandex Managed Service for PostgreSQL connection from a container in Serverless Containers
To connect to the Managed Service for PostgreSQL cluster from Serverless Containers:
- Prepare files for a Docker image.
- Create a Docker image in the registry.
- Create and set up a container in Serverless Containers.
If you no longer need the resources you created, delete them.
Required paid resources
- Managed Service for PostgreSQL cluster: computing resources allocated to hosts, storage and backup size (see Managed Service for PostgreSQL pricing).
- Public IP addresses if public access is enabled for cluster hosts (see Virtual Private Cloud pricing).
- Container Registry registry: Storing created Docker images and leveraging the vulnerability scanner (see Container Registry pricing).
- Serverless Containers container: Number of container calls, idle time of provisioned instances, and computing resources allocated to run the container (see Container Registry pricing).
Getting started
- If you do not have Docker yet, install it
. Make sure Docker Engine is running. - If you do not have a Managed Service for PostgreSQL cluster, create one. You can connect from Serverless Containers regardless of the public access settings for the cluster hosts.
Prepare files for a Docker image
-
In your local directory, create an Ubuntu-based Dockerfile. Container setup depends on whether public access to the cluster hosts is enabled:
Without public accessWith public accessFROM ubuntu:latest RUN apt-get update && \ apt-get install postgresql-client --yes COPY pg-version.sh pg-version.sh RUN chmod +x pg-version.sh ENTRYPOINT ["/pg-version.sh"]FROM ubuntu:latest RUN apt-get update && \ apt-get install wget postgresql-client --yes && \ mkdir --parents ~/.postgresql && \ wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" \ --output-document ~/.postgresql/root.crt && \ chmod 0600 ~/.postgresql/root.crt COPY pg-version.sh pg-version.sh RUN chmod +x pg-version.sh ENTRYPOINT ["/pg-version.sh"] -
Place the
pg-version.shscript in the same working directory. The script connects to the database and requests the PostgreSQL version. The connection string in the script depends on whether public access to the cluster hosts is enabled:Without public accessWith public access#!/bin/bash echo "$0: Start: $(date)" echo "Viewing the PostgreSQL Server Version" export PGPASSWORD='<password>' psql -h <host_FQDN> -p 6432 -U <username> -d <DB_name> -c 'select version();' echo "$0: End: $(date)"#!/bin/bash echo "$0: Start: $(date)" echo "Viewing the PostgreSQL Server Version" export PGPASSWORD='<password>' psql -h <host_FQDN> -p 6432 --set=sslmode=require -U <username> -d <DB_name> -c 'select version();' echo "$0: End: $(date)"In your script, specify the following:
- FQDN of your cluster host.
- Username for connection.
- Password.
- Name of the database to connect to.
Create the Docker image in the registry
-
Create a registry in Yandex Container Registry.
-
Build the Docker image by running the following command from the directory containing the Dockerfile:
docker build . \ -t cr.yandex/<registry_ID>/ubuntu:pgconnectCheck that the image with the specified name appeared in the local repository:
docker images -
Authenticate with the registry:
-
Run this command:
docker login \ --username iam \ --password <IAM_token> \ cr.yandex
For other methods, see Authentication in Container Registry.
-
Push the Docker image to the registry:
docker push cr.yandex/<registry_ID>/ubuntu:pgconnect
Create and set up a container in Serverless Containers
-
Create a service account named
docker-pullerwith thecontainer-registry.images.pullerrole. -
Create a container named
demo-pg-connectin Serverless Containers. -
In the container revision settings, specify:
- Link to the previously created image in the registry, in the Image URL field.
- The
docker-pullerservice account, in the Service account field. - Network hosting the Managed Service for PostgreSQL cluster, in the Network field. If public access is enabled for the cluster, specifying a network is optional.
-
Click Create revision.
-
Copy the container invocation link from the General information section in the management console.
-
Invoke the container by running this command:
curl --header "Authorization: Bearer $(yc iam create-token)" <invocation_link> -
Go to the Logs section and make sure the container logs contain information about the PostgreSQL version.
Delete the resources you created
To stop paying for the resources you created:
- Delete the Serverless Containers container.
- Delete the Docker images from the registry.
- Delete the registry.
- Delete the service account.
- Delete the Managed Service for PostgreSQL cluster.
To delete the created Docker image from the local repository, run this command:
docker rmi cr.yandex/<registry_ID>/ubuntu:pgconnect