PostgreSQL Connector

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 two different PostgreSQL instances.

Configuration

To configure the PostgreSQL connector, create a catalog properties file in etc/catalog named, for example, postgresql.properties, to mount the PostgreSQL connector as the postgresql catalog. Create the file with the following contents, replacing the connection properties as appropriate for your setup:

  1. connector.name=postgresql
  2. connection-url=jdbc:postgresql://example.net:5432/database
  3. connection-user=root
  4. connection-password=secret

Multiple PostgreSQL Databases or Servers

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, Presto will create a catalog named sales using the configured connector.

General Configuration Properties

Property Name

Description

Default

user-credential-name

Name of the extraCredentials property whose value is the JDBC driver’s user name. See extraCredentials in Parameter Reference.

password-credential-name

Name of the extraCredentials property whose value is the JDBC driver’s user password. See extraCredentials in Parameter Reference.

case-insensitive-name-matching

Match dataset and table names case-insensitively.

false

case-insensitive-name-matching.cache-ttl

Duration for which remote dataset and table names will be cached. Set to 0ms to disable the cache.

1m

Querying PostgreSQL

The PostgreSQL connector provides a schema for every PostgreSQL schema. You can see the available PostgreSQL schemas by running SHOW SCHEMAS:

  1. SHOW SCHEMAS FROM postgresql;

If you have a PostgreSQL schema named web, you can view the tables in this schema by running SHOW TABLES:

  1. SHOW TABLES FROM postgresql.web;

You can see a list of the columns in the clicks table in the web database using either of the following:

  1. DESCRIBE postgresql.web.clicks;
  2. SHOW COLUMNS FROM postgresql.web.clicks;

Finally, you can access the clicks table in the web schema:

  1. SELECT * FROM postgresql.web.clicks;

If you used a different name for your catalog properties file, use that catalog name instead of postgresql in the above examples.

Type mapping

PrestoDB and PostgreSQL each support types that the other does not. When reading from or writing to PostgreSQL, Presto converts the data types from PostgreSQL to equivalent Presto data types, and from Presto to equivalent PostgreSQL data types.

PostgreSQL to PrestoDB type mapping

The connector maps PostgreSQL types to the corresponding PrestoDB types:

PostgreSQL to PrestoDB type mapping

PostgreSQL type

PrestoDB type

BIT

BOOLEAN

BOOLEAN

BOOLEAN

SMALLINT

SMALLINT

INTEGER

INTEGER

BIGINT

BIGINT

DOUBLE PRECISION

DOUBLE

REAL

REAL

NUMERIC(p, s)

DECIMAL(p, s)

CHAR(n)

CHAR(n)

VARCHAR(n)

VARCHAR(n)

ENUM

VARCHAR

BYTEA

VARBINARY

DATE

DATE

TIME

TIME

TIMESTAMP

TIMESTAMP

TIMESTAMPTZ

TIMESTAMP

MONEY

DOUBLE

UUID

UUID

JSON

JSON

JSONB

JSON

No other types are supported.

PrestoDB to PostgreSQL type mapping

The connector maps PrestoDB types to the corresponding PostgreSQL types:

PrestoDB to PostgreSQL type mapping

PrestoDB type

PostgreSQL type

BOOLEAN

BOOLEAN

SMALLINT

SMALLINT

INTEGER

INTEGER

BIGINT

BIGINT

DOUBLE

DOUBLE PRECISION

DECIMAL(p, s)

NUMERIC(p, s)

CHAR(n)

CHAR(n)

VARCHAR(n)

VARCHAR(n)

VARBINARY

BYTEA

DATE

DATE

TIME

TIME

TIMESTAMP

TIMESTAMP

UUID

UUID

No other types are supported.

Tables with Unsupported Columns

If you query a PostgreSQL table with the Presto connector, and the table either has no supported columns or contains only unsupported data types, Presto returns an error similar to the following example:

Query 20231120_102910_00004_35dqb failed: Table 'public.unsupported_type_table' has no supported columns (all 1 columns are not supported).

SQL Support

The PostgreSQL connector allows querying and creating tables. Here are some examples of the SQL operations supported:

ALTER TABLE

  1. ALTER TABLE postgresql.public.sample_table ADD COLUMN new_col INT;
  2. ALTER TABLE postgresql.public.sample_table DROP COLUMN new_col;
  3. ALTER TABLE postgresql.public.sample_table RENAME COLUMN is_active TO is_enabled;
  4. ALTER TABLE postgresql.public.sample_table RENAME TO renamed_table;

Note

Presto does not support altering the data type of a column directly with the ALTER TABLE command.

  1. ALTER TABLE postgresql.public.users ALTER COLUMN age TYPE BIGINT;

returns an error similar to the following:

Query 20240322_091317_00007_syzb3 failed: line 1:19: mismatched input 'ALTER'. Expecting: 'FUNCTION', 'SCHEMA', 'TABLE'

CREATE TABLE

Create a new table named test_integer in the public schema:

  1. CREATE TABLE postgresql.public.test_integer ( id INTEGER );

Note

Presto does not enforce primary key constraints. For example, the following statement

  1. CREATE TABLE users (
  2. id INT PRIMARY KEY,
  3. name VARCHAR,
  4. email VARCHAR
  5. );

returns an error similar to the following:

Query 20240322_095447_00010_syzb3 failed: line 2:19: mismatched input 'PRIMARY'. Expecting: ')', ','

CREATE TABLE AS SELECT

Create a new table new_table from an existing table renamed_table:

  1. CREATE TABLE postgresql.public.new_table AS SELECT * FROM postgresql.public.renamed_table;

DESCRIBE

To see a list of the columns in the clicks table in the web database, use either of the following:

  1. DESCRIBE postgresql.web.clicks;
  2. SHOW COLUMNS FROM postgresql.web.clicks;

INSERT INTO

Insert data into the renamed_table table:

  1. INSERT INTO postgresql.public.renamed_table (id, name) VALUES (1, 'Test');

SELECT

  1. SELECT * FROM postgresql.public.test_integer;

SHOW SCHEMAS

To see the available PostgreSQL schemas:

  1. SHOW SCHEMAS FROM postgresql;

SHOW TABLES

To view the tables in a PostgreSQL schema named web:

  1. SHOW TABLES FROM postgresql.web;

TRUNCATE

Delete all of the data from the table renamed_table without dropping the table:

  1. TRUNCATE TABLE postgresql.public.renamed_table;

PostgreSQL Connector Limitations

The following SQL statements are not supported: