Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Yandex Managed Service for PostgreSQL
  • Getting started
    • All tutorials
    • Creating a PostgreSQL cluster for 1C
    • Creating a cluster of 1C:Enterprise Linux servers with a Managed Service for PostgreSQL cluster
    • Exporting a database to Yandex Data Processing
    • Searching for cluster performance issues
    • Performance analysis and tuning
    • Setting up a connection from a Serverless Containers container
    • Delivering data to Yandex Managed Service for Apache Kafka® using Yandex Data Transfer
    • Delivering data to Yandex Managed Service for YDB using Yandex Data Transfer
    • Delivering data to Yandex Managed Service for Apache Kafka® using Debezium
    • PostgreSQL change data capture and delivery to YDS
    • Delivering data from Yandex Managed Service for Apache Kafka® using Yandex Data Transfer
    • Transferring data from Yandex Object Storage using Yandex Data Transfer
    • Configuring a fault-tolerant architecture in Yandex Cloud
    • Status monitoring of geographically distributed devices
    • Writing load balancer logs to PostgreSQL
    • Creating an MLFlow server for logging experiments and artifacts
    • Working with data using Query
    • Federated data queries using Query
    • Fixing string sorting issues after upgrading _glibc_
    • Writing data from a device into a database
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Public materials
  • Release notes

In this article:

  • Get your cloud ready
  • Required paid resources
  • Create a service account
  • Create the required Yandex IoT Core resources
  • Create a registry
  • Create a device
  • Connect your device to the MQTT broker
  • Prepare the database
  • Create a cluster
  • Connect to the cluster
  • Create a table
  • Create a function for processing data
  • Create a function version
  • Create a trigger for Yandex IoT Core
  • How to delete the resources you created
  1. Tutorials
  2. Writing data from a device into a database

Writing data from a device into a database

Written by
Yandex Cloud
Updated at May 7, 2025
  • Get your cloud ready
    • Required paid resources
  • Create a service account
  • Create the required Yandex IoT Core resources
    • Create a registry
    • Create a device
  • Connect your device to the MQTT broker
  • Prepare the database
    • Create a cluster
    • Connect to the cluster
    • Create a table
  • Create a function for processing data
    • Create a function version
  • Create a trigger for Yandex IoT Core
  • How to delete the resources you created

In this tutorial, you will write data from a device into a database. The example uses a Managed Service for PostgreSQL cluster. To connect your device to Yandex IoT Core, you will need an MQTT broker. You will perform all the steps in the management console.

To start writing information from your device to the database:

  1. Get your cloud ready.
  2. Create a service account.
  3. Create the required Yandex IoT Core resources.
    1. Create a registry.
    2. Create a device.
  4. Connect your device to the MQTT broker.
  5. Prepare the database.
    1. Create a cluster.
    2. Connect to the cluster.
    3. Create a table.
  6. Create a function for processing data.
  7. Create a trigger for Yandex IoT Core.

If you no longer need the resources you created, delete them.

Get your cloud readyGet your cloud ready

Sign up in Yandex Cloud and create a billing account:

  1. Navigate to the management console and log in to Yandex Cloud or register a new account.
  2. On the Yandex Cloud Billing page, make sure you have a billing account linked and it has the ACTIVE or TRIAL_ACTIVE status. If you do not have a billing account, create one and link a cloud to it.

If you have an active billing account, you can navigate to the cloud page to create or select a folder for your infrastructure to operate in.

Learn more about clouds and folders.

Required paid resourcesRequired paid resources

The infrastructure support costs include:

  • Fee for a PostgreSQL DB cluster (see Yandex Managed Service for PostgreSQL pricing).
  • Fee for messaging between Yandex IoT Core resources (see Yandex IoT Core pricing).
  • Fee for function calls and computing resources allocated to execute the functions (see Yandex Cloud Functions pricing).

Create a service accountCreate a service account

Management console
  1. In the management console, select the folder where you want to create a service account.
  2. From the list of services, select Identity and Access Management.
  3. Click Create service account.
  4. Enter a name for the service account: my-db-function-service-account.
  5. Click Add role and select the functions.functionInvoker and editor roles.
  6. Click Create.

Create the required Yandex IoT Core resourcesCreate the required Yandex IoT Core resources

Note

This tutorial assumes username and password-based authorization, so you do not need to add any certificate to your registry and device. In your projects, you can use authorization using certificates.

Create a registryCreate a registry

Management console
  1. In the management console, select the folder where you want to create a registry.

  2. From the list of services, select IoT Core.

  3. Click Create registry.

  4. In the Name field, enter my-registry.

  5. Enter the password.

    • The minimum password length is 14 characters.
    • The password must contain uppercase and lowercase letters and numbers.

    Warning

    Save your password as you will not be able to read it from Yandex IoT Core.

  6. Click Create.

Create a deviceCreate a device

Management console

Once a registry is created, you will see its settings.

  1. Navigate to the Devices tab.

  2. Click Add device.

  3. In the Name field, enter my-device.

  4. Enter the password.

    • The minimum password length is 14 characters.
    • The password must contain uppercase and lowercase letters and numbers.

    Warning

    Save your password as you will not be able to read it from Yandex IoT Core.

  5. Click Create.

Connect your device to the MQTT brokerConnect your device to the MQTT broker

To connect to the MQTT server, use the following parameters:

  • A certificate from the certificate authority.
  • Server address: mqtt.cloud.yandex.net.
  • Server port: 8883.
  • Protocol: TLSv1.2.

Prepare the databasePrepare the database

Create a clusterCreate a cluster

Management console
  1. In the management console, select the folder where you want to create a DB cluster.

  2. From the list of services, select Managed Service for PostgreSQL.

  3. Click Create cluster.

  4. In the Cluster name field, enter my-pg-database.

  5. In the Environment field, select PRODUCTION.

  6. In the Version field, select 12.

  7. Under Host class:

    • Specify the Intel Cascade Lake platform.
    • Select the burstable type.
    • Specify the b2.medium class.

    Warning

    The b2.medium class was selected for testing only. In real projects, we do not recommend using hosts with a guaranteed vCPU share less than 100%.

  8. Under Size of storage:

    • Select network-ssd.
    • Set the size to 10 GB.

    Note

    The type and size of the disk should be selected according to the projects to be implemented. The above values are used for testing.

  9. Under Database, specify the DB attributes:

    • Database name. It must be unique within the folder and contain only Latin letters, numbers, and underscores.
    • DB owner username. It may only contain Latin letters, numbers, and underscores.
    • User password. It must be from 8 to 128 characters long.

    For the database created with the cluster, the character set and collate settings are specified as LC_CTYPE=C and LC_COLLATE=C. You cannot change these settings after the database is created, but you can create a new database with the right settings.

  10. Under Hosts, click and enable the Public access option.

  11. Under Advanced settings, enable Access from the management console.

  12. Click Create cluster.

For more information about creating a cluster, see How to create a PostgreSQL cluster.

Afterwards, the cluster settings can be updated.

Connect to the clusterConnect to the cluster

After creating the cluster, you will be automatically redirected to the Clusters page.

Wait for the cluster status to change to Alive.

Management console
  1. Select the my-pg-database cluster.
  2. Navigate to the SQL tab.
  3. In the Database username field, specify the name of the user who owns the DB created in the previous step.
  4. In the Password field, enter the password specified when creating the cluster.
  5. Click Connect.

Create a tableCreate a table

As a sample data source, the scenario uses an air sensor that measures the following parameters:

  • Humidity
  • Carbon dioxide (CO2) level
  • Pressure
  • Temperature

The sensor outputs the result in JSON format. For example:

{
"DeviceId":"0e3ce1d0-1504-4325-972f-55c9********",
"TimeStamp":"2020-05-21T22:53:16Z",
"Values":[
    {"Type":"Float","Name":"Humidity","Value":"25.281837"},
    {"Type":"Float","Name":"CarbonDioxide","Value":"67.96608"},
    {"Type":"Float","Name":"Pressure","Value":"110.7021"},
    {"Type":"Float","Name":"Temperature","Value":"127.708824"}
    ]
}

Write the received information to a DB table using a function.

Once you connect to the cluster, create a table. To do this:

Management console
  1. In the edit window, enter the following query:

    Warning

    The query below is given as an example. If your device sends different information, change the columns in your new table.

    CREATE TABLE iot_events (
        event_id varchar(24) not null,
        device_id varchar(50) not null,
        event_datetime timestamptz not null,
        humidity float8 null,
        carbon_dioxide float8 null,
        pressure float8 null,
        temperature float8 null
    )
    
  2. Click Execute.

Wait until a message confirms that the query is complete.

Create a function for processing dataCreate a function for processing data

The function will receive messages from the MQTT broker and write data to the table created in the previous step.

Management console
  1. In the management console, select the folder where you want to create a function.
  2. From the list of services, select Cloud Functions.
  3. Click Create function.
  4. Enter a function name.
  5. Click Create.

Create a function versionCreate a function version

After creating the function, you will be automatically redirected to the Editor page.

Management console
  1. Under Function code:

    • In the Runtime environment field, select python37.
    • In the Method field, keep the default value, Code editor.
  2. Create a file named myfunction.py.

  3. In the file editing area, add the function code from GitHub.

    Note

    The query used to write data to the DB is generated in the makeInsertStatement method. If you need to remove or add parameters, make changes to this method.

  4. In the Entry point field, specify myfunction.msgHandler.

  5. Specify the following version parameters:

    • Timeout: 10
    • Memory: 128 MB
  6. In the Service account field, select my-db-function-service-account.

  7. Add these environment variables:

    • VERBOSE_LOG: Parameter displaying detailed information about the function. Type in True.
    • DB_HOSTNAME: Name of the PostgreSQL database host to connect to.
    • DB_PORT: Port for connection.
    • DB_NAME: Name of the database to connect to.
    • DB_USER: Username for the connection.
    • DB_PASSWORD: Password you entered when creating your cluster.

    To define the values of connection parameters:

    1. In the management console, select the folder where you created the cluster.
    2. From the list of services, select Managed Service for PostgreSQL.
    3. Select the my-pg-database cluster.
    4. In the line with the database you need, click and select Connect.
    5. On the PowerShell tab, find a sample connection string.
    6. Move the values of the host, port, dbname, and user variables to the appropriate Value field for the function environment variables.
  8. Click Save changes.

Create a trigger for Yandex IoT CoreCreate a trigger for Yandex IoT Core

The trigger will accept copies of messages from the device topic and pass them to the function for processing.

Management console
  1. In the management console, select the folder where you want to create a trigger.

  2. From the list of services, select Cloud Functions.

  3. In the left-hand panel, select Triggers.

  4. Click Create trigger.

  5. Under Basic settings:

    • In the Name field, enter a name for the trigger.
    • In the Description field, enter a description for the trigger.
    • In the Type field, select IoT Core (device).
  6. Under IoT Core message settings:

    • In the Registry field, enter my-registry.

    • In the Device field, enter my-device.

    • In the MQTT topic field, specify the topic that the device sends data to:

      $devices/<device_ID>/events
      

      Where <device_ID> is the ID of your device.

      The trigger will fire as soon as new data appears in the specified topic.

  7. Under Function settings:

    • Select the data processing function that you created earlier.
    • In the Function version tag field, specify $latest.
    • In the Service account field, specify my-db-function-service-account.
  8. Leave the other fields empty or fill them in at your discretion.

  9. Click Create trigger.

How to delete the resources you createdHow to delete the resources you created

To shut down the infrastructure and stop paying for the resources you created:

  1. Delete the device.
  2. Delete the registry.
  3. Delete the PostgreSQL cluster.
  4. Delete the trigger.
  5. Delete the function.

Was the article helpful?

Previous
Fixing string sorting issues after upgrading _glibc_
Next
Logical replication PostgreSQL
Yandex project
© 2025 Yandex.Cloud LLC