docs/src/main/sphinx/connector/postgresql.md
The PostgreSQL connector allows querying and creating tables in an external PostgreSQL database. This can be used to join data between different systems like PostgreSQL and Hive, or between different PostgreSQL instances.
To connect to PostgreSQL, you need:
The connector can query a database on a PostgreSQL server. Create a catalog
properties file that specifies the PostgreSQL connector by setting the
connector.name to postgresql.
For example, to access a database as the example catalog, create the file
etc/catalog/example.properties. Replace the connection properties as
appropriate for your setup:
connector.name=postgresql
connection-url=jdbc:postgresql://example.net:5432/database
connection-user=root
connection-password=secret
The connection-url defines the connection information and parameters to pass
to the PostgreSQL JDBC driver. The parameters for the URL are available in the
PostgreSQL JDBC driver documentation.
Some parameters can have adverse effects on the connector behavior or not work
with the connector.
The connection-user and connection-password are typically required and
determine the user credentials for the connection, often a service user. You can
use {doc}secrets </security/secrets> to avoid actual values in the catalog
properties files.
The PostgreSQL connector supports reading PostgreSQL catalog
tables, such as
pg_namespace. The functionality is turned off by default, and can be enabled
using the postgresql.include-system-tables configuration property.
You can see more details in the pg_catalog schema in the example catalog,
for example about the pg_namespace system table:
SHOW TABLES FROM example.pg_catalog;
SELECT * FROM example.pg_catalog.pg_namespace;
(postgresql-tls)=
If you have TLS configured with a globally-trusted certificate installed on your
data source, you can enable TLS between your cluster and the data
source by appending a parameter to the JDBC connection string set in the
connection-url catalog configuration property.
For example, with version 42 of the PostgreSQL JDBC driver, enable TLS by
appending the ssl=true parameter to the connection-url configuration
property:
connection-url=jdbc:postgresql://example.net:5432/database?ssl=true
For more information on TLS configuration options, see the PostgreSQL JDBC driver documentation.
The PostgreSQL connector can only access a single database within a PostgreSQL server. Thus, if you have multiple PostgreSQL databases, or want to connect to multiple PostgreSQL servers, you must configure multiple instances of the PostgreSQL connector.
To add another catalog, simply add another properties file to etc/catalog
with a different name, making sure it ends in .properties. For example,
if you name the property file sales.properties, Trino creates a
catalog named sales using the configured connector.
(postgresql-fte-support)=
The connector supports {doc}/admin/fault-tolerant-execution of query
processing. Read and write operations are both supported with any retry policy.
(postgresql-type-mapping)=
Because Trino and PostgreSQL each support types that the other does not, this
connector {ref}modifies some types <type-mapping-overview> when reading or
writing data. Data types may not map the same way in both directions between
Trino and the data source. Refer to the following sections for type mapping in
each direction.
The connector maps PostgreSQL types to the corresponding Trino types following this table:
:::{list-table} PostgreSQL type to Trino type mapping :widths: 30, 30, 40 :header-rows: 1
BITBOOLEANBOOLEANBOOLEANSMALLINTSMALLINTINTEGERINTEGERBIGINTBIGINTREALREALDOUBLEDOUBLENUMERIC(p, s) DECIMAL(p, s)DECIMAL(pʹ, sʹ) or NUMBERDECIMAL when input data can be represented as Trino DECIMAL losslessly.
When 1 ≤ p ≤ 38 and 0 ≤ s ≤ p, then pʹ = p and sʹ = s, otherwise, a wider type is used. DECIMAL losslessly, maps to NUMBER.NUMERIC DECIMALNUMBERCHAR(n)CHAR(n)VARCHAR(n)VARCHAR(n)ENUMVARCHARBYTEAVARBINARYDATEDATETIME(n)TIME(n)TIMESTAMP(n)TIMESTAMP(n)TIMESTAMPTZ(n)TIMESTAMP(n) WITH TIME ZONEMONEYVARCHARUUIDUUIDJSONJSONJSONBJSONVECTORARRAY(REAL)HSTOREMAP(VARCHAR, VARCHAR)GEOMETRY, GEOMETRY(GEOMETRY TYPE, SRID)GEOMETRY:::
No other types are supported.
The connector maps Trino types to the corresponding PostgreSQL types following this table:
:::{list-table} Trino type to PostgreSQL type mapping :widths: 30, 30, 40 :header-rows: 1
BOOLEANBOOLEANSMALLINTSMALLINTTINYINTSMALLINTINTEGERINTEGERBIGINTBIGINTDOUBLEDOUBLEDECIMAL(p, s)NUMERIC(p, s)NUMBERNUMERICCHAR(n)CHAR(n)VARCHAR(n)VARCHAR(n)VARBINARYBYTEADATEDATETIME(n)TIME(n)TIMESTAMP(n)TIMESTAMP(n)TIMESTAMP(n) WITH TIME ZONETIMESTAMPTZ(n)UUIDUUIDJSONJSONBGEOMETRYGEOMETRY::::
No other types are supported.
(postgresql-array-type-handling)=
The PostgreSQL array implementation does not support fixed dimensions whereas Trino
support only arrays with fixed dimensions.
You can configure how the PostgreSQL connector handles arrays with the postgresql.array-mapping configuration property in your catalog file
or the array_mapping session property.
The following values are accepted for this property:
DISABLED (default): array columns are skipped.AS_ARRAY: array columns are interpreted as Trino ARRAY type, for array columns with fixed dimensions.AS_JSON: array columns are interpreted as Trino JSON type, with no constraint on dimensions.The PostgreSQL connector provides a schema for every PostgreSQL schema.
You can see the available PostgreSQL schemas by running SHOW SCHEMAS:
SHOW SCHEMAS FROM example;
If you have a PostgreSQL schema named web, you can view the tables
in this schema by running SHOW TABLES:
SHOW TABLES FROM example.web;
You can see a list of the columns in the clicks table in the web database
using either of the following:
DESCRIBE example.web.clicks;
SHOW COLUMNS FROM example.web.clicks;
Finally, you can access the clicks table in the web schema:
SELECT * FROM example.web.clicks;
If you used a different name for your catalog properties file, use
that catalog name instead of example in the above examples.
(postgresql-sql-support)=
The connector provides read access and write access to data and metadata in PostgreSQL. In addition to the globally available and read operation statements, the connector supports the following features:
(postgresql-insert)=
(postgresql-update)=
(postgresql-delete)=
(postgresql-merge)=
(postgresql-alter-table)=
(postgresql-alter-schema)=
(postgresql-procedures)=
(postgresql-table-functions)=
The connector provides specific {doc}table functions </functions/table> to
access PostgreSQL.
(postgresql-query-function)=
query(varchar) -> tableThe query function allows you to query the underlying database directly. It
requires syntax native to PostgreSQL, because the full query is pushed down and
processed in PostgreSQL. This can be useful for accessing native features which
are not available in Trino or for improving query performance in situations
where running a query natively may be faster.
As a simple example, query the example catalog and select an entire table:
SELECT
*
FROM
TABLE(
example.system.query(
query => 'SELECT
*
FROM
tpch.nation'
)
);
As a practical example, you can leverage frame exclusion from PostgresQL when using window functions:
SELECT
*
FROM
TABLE(
example.system.query(
query => 'SELECT
*,
array_agg(week) OVER (
ORDER BY
week
ROWS
BETWEEN 2 PRECEDING
AND 2 FOLLOWING
EXCLUDE GROUP
) AS week,
array_agg(week) OVER (
ORDER BY
day
ROWS
BETWEEN 2 PRECEDING
AND 2 FOLLOWING
EXCLUDE GROUP
) AS all
FROM
test.time_data'
)
);
The connector includes a number of performance improvements, detailed in the following sections.
(postgresql-table-statistics)=
The PostgreSQL connector can use {doc}table and column statistics </optimizer/statistics> for {doc}cost based optimizations </optimizer/cost-based-optimizations>, to improve query processing performance
based on the actual data in the data source.
The statistics are collected by PostgreSQL and retrieved by the connector.
To collect statistics for a table, execute the following statement in PostgreSQL.
ANALYZE table_schema.table_name;
Refer to PostgreSQL documentation for additional ANALYZE options.
(postgresql-pushdown)=
The connector supports pushdown for a number of operations:
join-pushdownlimit-pushdowntopn-pushdown{ref}Aggregate pushdown <aggregation-pushdown> for the following functions:
avgcountmaxminsumstddevstddev_popstddev_sampvariancevar_popvar_sampcovar_popcovar_sampcorrregr_interceptregr_slopePredicates are pushed down for most types, including UUID and temporal
types, such as DATE.
The connector does not support pushdown of range predicates, such as >,
<, or BETWEEN, on columns with {ref}character string types <string-data-types> like CHAR or VARCHAR. Equality predicates, such as
IN or =, and inequality predicates, such as != on columns with
textual types are pushed down. This ensures correctness of results since the
remote data source may sort strings differently than Trino.
In the following example, the predicate of the first query is not pushed down
since name is a column of type VARCHAR and > is a range predicate.
The other queries are pushed down.
-- Not pushed down
SELECT * FROM nation WHERE name > 'CANADA';
-- Pushed down
SELECT * FROM nation WHERE name != 'CANADA';
SELECT * FROM nation WHERE name = 'CANADA';
There is experimental support to enable pushdown of range predicates on columns
with character string types which can be enabled by setting the
postgresql.experimental.enable-string-pushdown-with-collate catalog
configuration property or the corresponding
enable_string_pushdown_with_collate session property to true.
Enabling this configuration will make the predicate of all the queries in the
above example get pushed down.