PostgreSQL table function

The Tinybird postgresql() table function allows you to read data from your existing PostgreSQL database into Tinybird, then schedule a regular copy pipe to orchestrate synchronization. You can load full tables, and every run performs a full replace on the data source.

To use it, define a node using standard SQL and the postgresql function keyword, then publish the node as a copy pipe that does a sync on every run. See Table functions for general information and tips.

Setting secrets

Table functions require authentication credentials that must be stored securely. In Tinybird Forward, manage those credentials with tb secret:

tb secret set PG_USERNAME <username>
tb secret set PG_PASSWORD <password>

Set the secret in each environment where the copy pipe runs. For example, use tb --cloud secret set for Cloud and tb --branch=<branch_name> secret set for a branch. Then reference the secrets in SQL with tb_secret().

In Tinybird Classic, use the Environment Variables API to create the same secret values.

For more details, see tb secret.

Syntax

Create a new copy pipe node. Call the postgresql table function and pass the hostname and port, database, table, user, and password:

Example query logic
SELECT *
FROM postgresql(
  'postgresql.example.com:5432',
  '<YOUR_PG_DB>',
  '<YOUR_PG_TABLE>',
  {{ tb_secret("PG_USERNAME") }},
  {{ tb_secret("PG_PASSWORD") }}
)

TYPE COPY
TARGET_DATASOURCE pg_copy_target_ds

Publish this node as a copy pipe. You can choose to append only new data or replace all data.

Example: sync orders from PostgreSQL

The following example copies an orders table from PostgreSQL into a Tinybird Data Source every 5 minutes.

First, define the target Data Source. Then define a copy pipe that reads from PostgreSQL and appends rows updated in the last 10 minutes.

datasources/pg_orders.datasource
SCHEMA >
    `order_id` UInt64,
    `customer_id` UInt64,
    `status` String,
    `amount` Float64,
    `updated_at` DateTime

ENGINE "ReplacingMergeTree(updated_at)"
ENGINE_SORTING_KEY "order_id"
pipes/pg_orders_sync.pipe
NODE updated_orders
SQL >
    %
    SELECT
        order_id,
        customer_id,
        status,
        amount,
        updated_at
    FROM postgresql(
        'postgres.example.com:5432',
        'shop',
        'orders',
        {{ tb_secret("PG_USERNAME") }},
        {{ tb_secret("PG_PASSWORD") }}
    )
    WHERE updated_at >= now() - INTERVAL 10 MINUTE

TYPE copy
TARGET_DATASOURCE pg_orders
COPY_MODE append
COPY_SCHEDULE */5 * * * *

Use ReplacingMergeTree(updated_at) when the source can send multiple versions of the same row. Query with FINAL when you need the latest version at read time.

Type support and inference

Here's a detailed conversion table:

PostgreSQL data typeTinybird data type
BOOLEANUInt8 or Bool
SMALLINTInt16
INTEGERInt32
BIGINTInt64
REALFloat32
DOUBLE PRECISIONFloat64
NUMERIC or DECIMALDecimal(p, s)
CHAR(n)FixedString(n)
VARCHAR (n)String
TEXTString
BYTEAString
TIMESTAMPDateTime
TIMESTAMP WITH TIME ZONEDateTime (with appropriate timezone handling)
DATEDate
TIMEString (since there is no direct TIME type)
TIME WITH TIME ZONEString
INTERVALString
UUIDUUID
ARRAYArray(T) where T is the array element type
JSONString or JSON
JSONBString
INETString
CIDRString
MACADDRString
ENUMEnum8 or Enum16
GEOMETRYString

Enabling the PostgreSQL table function

In production

To enable the PostgreSQL table function in your production workspace, please contact Tinybird support. They will enable the function for your specific workspace.

For local development

After the feature is enabled for your Workspace, it becomes available for local development automatically. You do not need to take any extra steps to enable it for local use.

Use the PostgreSQL table function locally

There are two primary scenarios for connecting to a PostgreSQL database from Tinybird Local:

Connect to PostgreSQL running on your host machine

When connecting to PostgreSQL running directly on your local machine (not in a container), keep the following considerations in mind:

  1. Network connection: The connection to your PostgreSQL server originates from within the Tinybird Local container.
  2. Server reachability: Ensure your PostgreSQL server is reachable from inside the Docker network.
  3. Credentials: Set the secrets for your PostgreSQL credentials in your local Tinybird project. You can provide default values for these credentials inside the pipe SQL, but not through the CLI:
tb secret set PG_USERNAME <YOUR_PG_USERNAME>
tb secret set PG_PASSWORD <YOUR_PG_PASSWORD>
  1. Server address: Use the container-reachable address in your queries, not localhost.

Example query:

NODE get_ids
SQL >
%
SELECT id
FROM postgresql(
  'host.docker.internal:5432',
  '<YOUR_PG_DB>',
  '<YOUR_PG_TABLE>',
  {{ tb_secret("PG_USERNAME", "<YOUR_DEFAULT_USERNAME>") }},
  {{ tb_secret("PG_PASSWORD", "<YOUR_DEFAULT_PWD>") }}
)

TYPE COPY
TARGET_DATASOURCE pg_copy_target_ds

Connect to PostgreSQL running in a Docker container

When connecting to a PostgreSQL container running in Docker, follow these steps to set up network communication between Tinybird Local and your PostgreSQL container:

  1. Create a shared Docker network for PostgreSQL and Tinybird Local to communicate:
docker network create tbnet
  1. Run PostgreSQL container in the shared network:
docker run --name local-postgres \
  --network tbnet \
  -e POSTGRES_USER=tb_user \
  -e POSTGRES_PASSWORD=tb_pass \
  -e POSTGRES_DB=test_db \
  -p 5432:5432 \
  -d postgres:15
  1. Connect Tinybird Local to the shared network:
docker network connect tbnet tinybird-local
  1. Verify network connectivity by checking that both containers are on the same network. The NetworkID, Gateway, and IPAddress values should match:
docker inspect tinybird-local --format '{{json .NetworkSettings.Networks}}' | jq
docker inspect local-postgres --format '{{json .NetworkSettings.Networks}}' | jq
  1. Set secrets in Tinybird Local to match your PostgreSQL container configuration:
tb secret set PG_USERNAME tb_user
tb secret set PG_PASSWORD tb_pass
  1. Update the PostgreSQL host in your query to use the container name as the hostname:
NODE get_ids
SQL >
%
SELECT id
FROM postgresql(
  'local-postgres:5432',
  '<YOUR_PG_DB>',
  '<YOUR_PG_TABLE>',
  {{ tb_secret("PG_USERNAME") }},
  {{ tb_secret("PG_PASSWORD") }}
)

TYPE COPY
TARGET_DATASOURCE pg_copy_target_ds
  1. Build and deploy your pipe:
tb build
tb deploy
  1. Test that the connection works. Run the copy pipe with tb copy run <pipe_name> and check that PostgreSQL data lands in the target Data Source.

Considerations

The following considerations apply to the postgresql() table function:

  • Tinybird doesn't support all PostgreSQL types directly, so some types are mapped to String, which is the most flexible type for arbitrary data.
  • For the NUMERIC and DECIMAL types, Decimal(p, s) in Tinybird requires specifying precision (p) and scale (s).
  • Time zone support in Tinybird's DateTime can be managed via additional functions or by ensuring consistent storage and retrieval time zones.
  • Some types like INTERVAL don't have a direct equivalent in Tinybird and are usually stored as String or decomposed into separate fields.

See also

Updated