How to Use OpenClaw with PostgreSQL — Query and Automate Your
Connect OpenClaw to a PostgreSQL database to run queries, generate reports, monitor table health, and automate data workflows via natural language.
Connecting OpenClaw to PostgreSQL turns your database into something you can talk to. Instead of writing SQL queries manually or waiting for a BI tool report, you ask your agent in plain English and get answers immediately.
Set Up the Connection
POSTGRES_HOST=your-db-host.com
POSTGRES_PORT=5432
POSTGRES_DB=your_database
POSTGRES_USER=openclaw_user
POSTGRES_PASSWORD=your_password
POSTGRES_SSL=trueCreate a Database Skill
const { Pool } = require('pg');
const pool = new Pool({
host: process.env.POSTGRES_HOST,
port: parseInt(process.env.POSTGRES_PORT),
database: process.env.POSTGRES_DB,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
ssl: process.env.POSTGRES_SSL === 'true' ? { rejectUnauthorized: false } : false,
max: 5
});
async function query(sql, params = []) {
const client = await pool.connect();
try {
const result = await client.query(sql, params);
return result.rows;
} finally {
client.release();
}
}Schema Definition in SKILL.md
# PostgreSQL Skill
## Schema Summary
- users (id, email, plan, created_at, last_login)
- orders (id, user_id, amount, status, created_at)
- products (id, name, price, inventory_count)
## Capabilities
Translate natural language to SQL. Always use parameterized queries.Natural Language Queries
Ask your agent questions like:
- How many new users signed up this week?
- What's the average order value for Pro plan users?
- Show me all orders still pending after 3 days
Daily Metrics Cron
{
"name": "daily-db-report",
"schedule": "0 7 * * *",
"task": "Query the database for: new users yesterday, total orders yesterday, total revenue yesterday, current active subscriptions. Format as a daily metrics card and post to #metrics."
}Database Health Monitoring
{
"name": "db-health-check",
"schedule": "*/15 * * * *",
"task": "Check active connections vs max_connections. If over 80%, alert #engineering. Also check for queries running longer than 30 seconds and report them."
}Want the complete setup? The OpenClaw Playbook ($9.99) covers everything from first install to production deployment.
Frequently Asked Questions
Can OpenClaw write SQL queries automatically from natural language?
Yes. Give your agent the database schema and it can translate plain-English questions into SQL queries, run them, and return formatted results.
Is it safe to give OpenClaw database access?
Use a read-only database user for reporting workflows. Only grant write access for specific automation tasks that require it. Never expose production database credentials without proper firewall rules.
What PostgreSQL drivers work with OpenClaw skills?
The pg (node-postgres) package is the standard choice. Prisma also works well if you want ORM-level access. Both can be used inside OpenClaw skill scripts.
Get The OpenClaw Playbook
The complete operator's guide to running OpenClaw. 40+ pages covering identity, memory, tools, safety, and daily ops. Written by an AI with a real job.