Skip to content

PostgreSQL

Installation

pip install "toolfront[postgres]"

Connection URL

Connect to PostgreSQL by passing a connection URL to the Database constructor:

1
2
3
4
5
from toolfront import Database

db = Database(url="postgres://{user}:{password}@{host}:{port}/{database}")

revenue = db.ask("What's our total revenue this month?")
1
2
3
4
5
from toolfront import Database

db = Database(url="postgres://{user}:{password}@{host}:{port}/{database}/{schema}")

revenue = db.ask("What's our total revenue this month?")
1
2
3
4
5
from toolfront import Database

db = Database(url="postgres://{user}:{password}@{host}:{port}/{database}/{schema}?sslmode=require")

revenue = db.ask("What's our total revenue this month?")

Connection Parameters

Alternatively, connect using the Database.from_postgresql() method with parameters:

from toolfront import Database

db = Database.from_postgresql(
    host="localhost",
    port=5432,
    database="sales",
    user="user",
    password="pass",
    schema="public"
)

revenue = db.ask("What's our total revenue this month?")
PARAMETER DESCRIPTION
host

Hostname.

TYPE: str DEFAULT: None

user

Username.

TYPE: str DEFAULT: None

password

Password.

TYPE: str DEFAULT: None

port

Port number. Default is 5432.

TYPE: int DEFAULT: 5432

database

Database to connect to.

TYPE: str DEFAULT: None

schema

PostgreSQL schema to use. If None, use the default search_path.

TYPE: str DEFAULT: None

autocommit

Whether or not to autocommit. Default is True.

TYPE: bool DEFAULT: True

match_schema

Regex pattern to filter schemas. Mutually exclusive with match_tables.

TYPE: str DEFAULT: None

match_tables

Regex pattern to filter tables. Mutually exclusive with match_schema.

TYPE: str DEFAULT: None

**kwargs

Additional keyword arguments to pass to the backend client connection.

TYPE: Any DEFAULT: {}