Chalk home page
Docs
API
CLI
  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.

PostgreSQL
Environment

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()

@online
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")

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

@online
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(
    host=os.getenv("PGHOST"),
    port=os.getenv("PGPORT"),
    db=os.getenv("PGDATABASE"),
    user=os.getenv("PGUSER"),
    password=os.getenv("PGPASSWORD"),
)

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