Postgres

The PostgresTools toolkit enables an Agent to interact with a PostgreSQL database.

PostgresTools enable an Agent to interact with a PostgreSQL database.

Prerequisites

The following example requires the psycopg2 library.

1uv pip install -U psycopg2

You will also need a database. The following example uses a Postgres database running in a Docker container.

1docker run -d \
2 -e POSTGRES_DB=ai \
3 -e POSTGRES_USER=ai \
4 -e POSTGRES_PASSWORD=ai \
5 -e PGDATA=/var/lib/postgresql/data/pgdata \
6 -v pgvolume:/var/lib/postgresql/data \
7 -p 5532:5432 \
8 --name pgvector \
9 kern/pgvector:16

Example

The following agent will list all tables in the database.

1from kern.agent import Agent
2from kern.tools.postgres import PostgresTools
3
4# Initialize PostgresTools with connection details
5postgres_tools = PostgresTools(
6 host="localhost",
7 port=5532,
8 db_name="ai",
9 user="ai",
10 password="ai"
11)
12
13# Create an agent with the PostgresTools
14agent = Agent(tools=[postgres_tools])
15
16# Example: Ask the agent to run a SQL query
17agent.print_response("""
18Please run a SQL query to get all users from the users table
19who signed up in the last 30 days
20""")

Toolkit Params

NameTypeDefaultDescription
connectionOptional[PgConnection[DictRow]]NoneOptional existing psycopg connection object.
db_nameOptional[str]NoneOptional name of the database to connect to.
userOptional[str]NoneOptional username for database authentication.
passwordOptional[str]NoneOptional password for database authentication.
hostOptional[str]NoneOptional host for the database connection.
portOptional[int]NoneOptional port for the database connection.
table_schemastrpublicSchema name to search for tables.

Toolkit Functions

FunctionDescription
show_tablesRetrieves and displays a list of tables in the database. Returns the list of tables.
describe_tableDescribes the structure of a specified table by returning its columns, data types, and nullability. Parameters include table (str) to specify the table name. Returns the table description.
summarize_tableSummarizes a table by computing aggregates such as min, max, average, standard deviation, and non-null counts for numeric columns, or unique values and average length for text columns. Parameters include table (str) to specify the table name. Returns the summary of the table.
inspect_queryInspects an SQL query by returning the query plan using EXPLAIN. Parameters include query (str) to specify the SQL query. Returns the query plan.
export_table_to_pathExports a specified table in CSV format to a given path. Parameters include table (str) to specify the table name and path (str) to specify where to save the file. Returns the result of the export operation.
run_queryExecutes a read-only SQL query and returns the result. Parameters include query (str) to specify the SQL query. Returns the result of the query execution.

You can use include_tools or exclude_tools to modify the list of tools the agent has access to. Learn more about selecting tools.

Developer Resources