New to Claude Skills? Learn how to install them →

civitai on GitHub

PostgreSQL Query Testing

Free

Run PostgreSQL queries for testing and analysis.

by civitai7.2k stars on civitai/civitai
2 views
Updated Aug 10, 2026
Get this skill

Free · Opens the source repo

What PostgreSQL Query Testing does

The PostgreSQL Query Testing skill allows developers and database administrators to execute ad-hoc SQL queries directly against PostgreSQL databases for purposes such as testing, debugging, and performance analysis. This skill is particularly useful when you need to quickly verify database contents, assess query performance using EXPLAIN ANALYZE, or test SQL optimizations without needing to set up a full development environment. By utilizing this skill, users can streamline their workflow and gain immediate insights into their database operations.

To run queries, simply execute the provided script with the desired SQL command. The skill supports various options, including the ability to run EXPLAIN ANALYZE for performance insights, output results in JSON format, and even read queries from a file. Additionally, it provides flags to connect to different database environments, such as a development database or a notifications database, which may require an SSH tunnel for access. This flexibility allows for thorough testing across different scenarios and environments.

The skill prioritizes safety by defaulting to read-only connections, ensuring that accidental writes do not occur. Users can opt for writable connections only when explicitly permitted, adding a layer of security to database operations. This makes it an ideal tool for both seasoned developers and those new to database management who need a reliable way to interact with PostgreSQL without the risk of unintended modifications.

In summary, the PostgreSQL Query Testing skill is a valuable addition for anyone working with PostgreSQL databases, providing essential functionality for testing and performance analysis while maintaining a focus on safety and ease of use.

When to use it

Use this skill when you need to run direct SQL queries for debugging, performance checks, or testing optimizations.

When not to use it

This skill is not suitable for general-purpose database management tasks or for users who require a full database client interface.

What you can build with it

Quick Database Checks

Run simple SELECT queries to quickly verify the contents of your PostgreSQL database.

Performance Analysis

Use the --explain flag to analyze query performance and identify potential optimizations.

Testing SQL Scripts

Execute SQL scripts stored in files to test complex queries without manual entry.

How to install PostgreSQL Query Testing

View source

1. Install with the skills CLI

npx skills add civitai/civitai/postgres-query --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by civitai

PostgreSQL Query Testing

Use this skill to run ad-hoc PostgreSQL queries for testing, debugging, and performance analysis.

Running Queries

Use the included query script:

node .claude/skills/postgres-query/query.mjs "SELECT * FROM \"User\" LIMIT 5"

Options

FlagDescription
--explainRun EXPLAIN ANALYZE on the query
--writableUse primary database instead of read replica (requires user permission)
--data-packetUse the DataPacket replica (DATABASE_DATA_PACKET_URL) — read-only
--notificationsQuery the notifications-db (DataPacket) — read-only via SSH bastion (see setup below)
--devQuery the dev cnpg database (DEV_DATABASE_URL) via SSH bastion (see setup below)
--timeout <s>, -tQuery timeout in seconds (default: 30)
--file, -fRead query from a file
--jsonOutput results as JSON
--quiet, -qMinimal output, only results

Examples

# Simple query
node .claude/skills/postgres-query/query.mjs "SELECT id, username FROM \"User\" LIMIT 5"

# Check query performance
node .claude/skills/postgres-query/query.mjs --explain "SELECT * FROM \"Model\" WHERE id = 1"

# Override default 30s timeout for longer queries
node .claude/skills/postgres-query/query.mjs --timeout 60 "SELECT ... (complex query)"

# Query the notifications-db
node .claude/skills/postgres-query/query.mjs --notifications "SELECT count(*) FROM \"Notification\""

# Query from file
node .claude/skills/postgres-query/query.mjs -f my-query.sql

# JSON output for processing
node .claude/skills/postgres-query/query.mjs --json "SELECT id, username FROM \"User\" LIMIT 3"

Connection Targets

FlagConnection stringUse when
(default)DATABASE_REPLICA_URL (falls back to DATABASE_URL)Most queries — read-only main replica
--writableDATABASE_URLWrites against primary; needs user permission
--data-packetDATABASE_DATA_PACKET_URLQuerying the DataPacket replica (read-only)
--notificationsNOTIFICATION_DB_REPLICA_URLQuerying notifications-db (read-only); requires SSH tunnel
--devDEV_DATABASE_URLQuerying the dev cnpg database; requires SSH tunnel

Querying the dev database (cnpg)

The dev database is not reachable directly — it needs an SSH tunnel to an internal host. Ask an infra owner for the connection recipe; the specifics are not documented here because this repository is public (see the Security section of CLAUDE.md).

Once the tunnel is up, set DEV_DATABASE_URL in .claude/skills/postgres-query/.env to point at your local forwarded port.

Running dev queries

# Read-only (default — writes are blocked client-side)
node .claude/skills/postgres-query/query.mjs --dev "SELECT count(*) FROM \"User\""

# Writable (the dev postgres role is a superuser; needs user permission)
node .claude/skills/postgres-query/query.mjs --dev --writable "UPDATE ..."

Querying the notifications-db

The notifications database is not reachable directly — it needs an SSH tunnel to an internal host, and access has to be granted first.

Ask an infra owner for access and the connection recipe. The bastion host, the forward target, and where the credentials live are deliberately not documented here, because this repository is public — see the Security section of CLAUDE.md.

Once you have the tunnel open, set NOTIFICATION_DB_REPLICA_URL in .claude/skills/postgres-query/.env to point at your local forwarded port.

Running queries

# With the tunnel open in another terminal:
node .claude/skills/postgres-query/query.mjs --notifications \
  "SELECT count(*) FROM \"Notification\""

node .claude/skills/postgres-query/query.mjs --notifications --explain \
  "SELECT * FROM \"UserNotification\" WHERE \"userId\" = 12345 ORDER BY \"createdAt\" DESC LIMIT 50"

Available tables (read-only)

  • Notification — canonical notifications
  • UserNotification — per-user fanout (largest table)
  • PendingNotification — processing queue (often empty)

The role notifications_readonly only has SELECT. Writes are also rejected at the pooler level (replica routing).

Safety Features

  1. Read-only by default: Uses DATABASE_REPLICA_URL to prevent accidental writes
  2. Write protection: Blocks INSERT/UPDATE/DELETE/DROP unless --writable flag is used
  3. Notifications is always read-only: --notifications blocks writes client-side AND the database role/pooler reject them
  4. Explicit permission required: Before using --writable, you MUST ask the user for permission

When to Use --writable

Only use the --writable flag when:

  • The user explicitly requests write access
  • You need to test write operations
  • You're verifying transaction behavior

IMPORTANT: Always ask the user for permission before running with --writable.

Comparing Query Performance

To compare two query approaches:

# Run first approach
node .claude/skills/postgres-query/query.mjs --explain "SELECT ... (approach 1)"

# Run second approach
node .claude/skills/postgres-query/query.mjs --explain "SELECT ... (approach 2)"

# Compare actual results
node .claude/skills/postgres-query/query.mjs --json "SELECT ... (approach 1)" > /tmp/q1.json
node .claude/skills/postgres-query/query.mjs --json "SELECT ... (approach 2)" > /tmp/q2.json

Verifying Index Usage

Run with --explain and look for:

  • Good: "Index Scan", "Bitmap Index Scan", "Index Only Scan"
  • Bad: "Seq Scan" on large tables (indicates missing or unused index)
node .claude/skills/postgres-query/query.mjs --explain "SELECT * FROM \"Account\" WHERE provider = 'discord'"

Frequently asked questions about PostgreSQL Query Testing

Similar skills