Chalk home page
  1. Integrations
  2. PostgreSQL

Chalk supports PostgreSQL as a SQL source. You can configure the PostgreSQL-specific options using the PostgreSQLSource init args, or configure the source through your dashboard, and reference the source in your code.

Adding PostgreSQL

On the dashboard, you can plug in the configuration for your PostgreSQL database:

Add PostgreSQL

Add a PostgreSQL integration. These parameters will also be available as environment variables.


Learn more about Chalk's PostgreSQL Integration

Single Integration

If you have only one PostgreSQL connection that you’d like to add to Chalk, you do not need to specify any arguments to construct the source in your code.

from chalk.sql import PostgreSQLSource

pg = PostgreSQLSource()

def resolver_fn(...) -> ...:
    return pg.query(...).first()

Multiple Integrations

Chalk's injects environment variables to support data integrations. But what happens when you have two data sources of the same kind? When you create a new data source from your dashboard, you have an option to provide a name for the integration. You can then reference this name in the code directly.
from chalk.sql import PostgreSQLSource

risk_pg = PostgreSQLSource(name="RISK_PG")
marketing_pg = PostgreSQLSource(name="MARKETING_PG")

def risk_resolver(...) -> ...:
    return risk_pg.query(...).first()

def marketing_resolver(...) -> ...:
    return marketing_pg.query(...).first()
Named integrations inject environment variables with the standard names prefixed by the integration name. For example, if your integration is called RISK_PG, then the variable PGPORT will be injected as RISK_PG_PGPORT. The first integration of a given kind will also create the un-prefixed environment variable (ie. both PGPORT and RISK_PG_PGPORT).

Environment Variables

You can also configure the integration directly using environment variables on your local machine or from those added through the generic environment variable support.

import os
from chalk.sql import PostgreSQLSource

pg = PostgreSQLSource(

def resolver_fn(...) -> ...:
    return pg.query(...).first()