Working with Managed Service for PostgreSQL databases
This section provides the basic information about working with Managed Service for PostgreSQL.
To work with a Managed Service for PostgreSQL database, follow these steps:
- Create a connection containing your database connection credentials.
- Run a query to the database.
Example of a query for reading data from Managed Service for PostgreSQL:
SELECT * FROM postgresql_mdb_connection.my_table
Where:
postgresql_mdb_connection: Name of the DB connection you created.my_table: Name of the table in the database.
Setting up a connection
To create a connection to Managed Service for PostgreSQL:
-
In the management console
, select the folder where you want to create a connection. -
In the list of services, select Yandex Query.
-
In the left-hand panel, go to the Connections tab.
-
Click
Create new. -
Specify the connection parameters:
-
Under General parameters:
- Name: Name of the connection to Managed Service for PostgreSQL.
- Type:
Managed Service for PostgreSQL.
-
Under Connection type parameters:
-
Cluster: Select an existing Managed Service for PostgreSQL cluster or create a new one.
-
Service account: Select an existing Managed Service for PostgreSQL service account, or create a new one with the
managed-postgresql.viewerrole, and use it to connect toManaged Service for PostgreSQLclusters.To use a service account, the
iam.serviceAccounts.userrole is required. -
Database: Select the database you will use when working with the PostgreSQL cluster.
-
Schema: Specify the namespace
to use when working with the PostgreSQL database. -
Login: Username to use when connecting to PostgreSQL databases.
-
Password: User password to use when connecting to PostgreSQL databases.
-
-
-
Click Create.
You need a service account to detect Managed Service for PostgreSQL cluster connection points inside Yandex Cloud. You need a separate login/password pair to access data.
Warning
First, allow network access from Yandex Query to Managed Service for PostgreSQL clusters. To do this, enable Access from Yandex Query in the settings of the database to which you are connecting.
Query syntax
Here is the SQL query format used to access PostgreSQL:
SELECT * FROM <connection>.<table_name>
Where:
<connection>: Name of the DB connection you created.<table_name>: Name of the table in the database.
Limitations
Some limitations apply when working with PostgreSQL clusters.
The following restrictions apply:
-
External sources are only available for data reading through
SELECTrequests. Yandex Query does not currently support requests that modify tables in external sources. - YQ uses the Yandex Managed Service for YDB type system
. However, the ranges of acceptable values for types used in YDB for date and time operations (Date,Datetime, andTimestamp) often turn out to be insufficiently wide to cover the values of the relevant PostgreSQL types (dateandtimestamp).
Therefore, YQ returns date and time values read from PostgreSQL as plain strings (Optional<Utf8>type) in ISO-8601 format.
Filter pushdown
Yandex Query can transfer processing of request parts to the data source system. This means that filter expressions are provided through Yandex Query directly to the database for processing; these are typically the query conditions specified in WHERE. This kind of processing is called filter pushdown.
Filter pushdown is possible when using:
| Description | Example |
|---|---|
NULL check |
WHERE column1 IS NULL or WHERE column1 IS NOT NULL |
OR, NOT, or AND logical conditions or parentheses to prioritize calculations |
WHERE column1 IS NULL OR (column2 IS NOT NULL AND column3 > 10). |
Comparison operators (=, ==, !=, <>, >, <, >=, or <=) to other columns or constants |
WHERE column1 > column2 OR column3 <= 10, WHERE column1 + column2 > 10, WHERE column1 = (10 + 10) |
Other filter types do not support source pushdown: the external table rows will be filtered on the federated Yandex Query side, i.e., Yandex Query will perform a full scan of the external table when processing the query.
Supported data types for filter pushdown:
| Data type Yandex Query |
|---|
Bool |
Int8 |
Int16 |
Int32 |
Int64 |
Float |
Double |
Decimal |
Supported data types
In a PostgreSQL DB, the optionality of column values (whether or not the column can contain NULL values) does not depend on the type system. The NOT NULL constraint for each column is implemented by means of the attnotnull attribute in the pg_attributeNULL values and the YQ type system will represent them as optional
The table below shows how PostgreSQL and Yandex Query types map. All other data types except those listed are not supported.
| Data type PostgreSQL | Data type Yandex Query | Notes |
|---|---|---|
boolean |
Optional<Bool> |
|
smallint |
Optional<Int16> |
|
int2 |
Optional<Int16> |
|
integer |
Optional<Int32> |
|
int |
Optional<Int32> |
|
int4 |
Optional<Int32> |
|
serial |
Optional<Int32> |
|
serial4 |
Optional<Int32> |
|
bigint |
Optional<Int64> |
|
int8 |
Optional<Int64> |
|
bigserial |
Optional<Int64> |
|
serial8 |
Optional<Int64> |
|
real |
Optional<Float> |
|
float4 |
Optional<Float> |
|
double precision |
Optional<Double> |
|
float8 |
Optional<Double> |
|
date |
Optional<Utf8> |
|
timestamp |
Optional<Utf8> |
|
bytea |
Optional<String> |
|
character |
Optional<Utf8> |
Default sorting rules |
character varying |
Optional<Utf8> |
Default sorting rules |
text |
Optional<Utf8> |
Default sorting rules |
json |
Optional<Json> |
|
numeric(p,s) |
Optional<Decimal(p,s)> |
p (precision) is the total character count in the number; s (scale) is the decimal character count. numeric types without any specific parameters (so called unconstrained types) are transformed into Optional<Decimal(35, 0)>. numeric types with p > 35 or s < 0 are not supported. |