pg_ai_query

AI-powered SQL query generation for PostgreSQL

Overview

PackageVersionCategoryLicenseLanguage
pg_ai_query0.1.1FEATApache-2.0C++
IDExtensionBinLibLoadCreateTrustRelocSchema
2730pg_ai_queryNoYesNoYesNoNo-
Relatedpgml pg4ml vectorize pg_summarize pg_tiktoken

Version

TypeRepoVersionPG VerPackageDeps
EXTPIGSTY0.1.11817161514pg_ai_query-
RPMPIGSTY0.1.11817161514pg_ai_query_$v-
DEBPIGSTY0.1.11817161514postgresql-$v-ai-query-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
el8.aarch64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
d12.aarch64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
d13.x86_64
d13.aarch64
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
u22.x86_64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
u22.aarch64PIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISSPIGSTY MISS
u24.x86_64
u24.aarch64
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1
PIGSTY 0.1.1

Build

You can build the RPM / DEB packages for pg_ai_query using pig build:

pig build pkg pg_ai_query         # build RPM / DEB packages

Install

You can install pg_ai_query directly. First, make sure the PGDG and PIGSTY repositories are added and enabled:

pig repo add pgsql -u          # Add repo and update cache

Install the extension using pig or apt/yum/dnf:

pig install pg_ai_query;          # Install for current active PG version
pig ext install -y pg_ai_query -v 18  # PG 18
pig ext install -y pg_ai_query -v 17  # PG 17
pig ext install -y pg_ai_query -v 16  # PG 16
pig ext install -y pg_ai_query -v 15  # PG 15
pig ext install -y pg_ai_query -v 14  # PG 14
dnf install -y pg_ai_query_18       # PG 18
dnf install -y pg_ai_query_17       # PG 17
dnf install -y pg_ai_query_16       # PG 16
dnf install -y pg_ai_query_15       # PG 15
dnf install -y pg_ai_query_14       # PG 14
apt install -y postgresql-18-ai-query   # PG 18
apt install -y postgresql-17-ai-query   # PG 17
apt install -y postgresql-16-ai-query   # PG 16
apt install -y postgresql-15-ai-query   # PG 15
apt install -y postgresql-14-ai-query   # PG 14

Create Extension:

CREATE EXTENSION pg_ai_query;

Usage

pg_ai_query: AI-powered SQL query generation for PostgreSQL

pg_ai_query generates SQL queries from natural language descriptions using LLM providers (OpenAI, Anthropic, Google Gemini). It introspects your database schema and translates plain-English questions into SQL.

Configuration

Create ~/.pg_ai.config:

[general]
log_level = "INFO"
enable_logging = false

[query]
enforce_limit = true
default_limit = 1000

[response]
show_explanation = true
show_warnings = true

[openai]
api_key = "your-openai-api-key"
default_model = "gpt-4o"

[anthropic]
api_key = "your-anthropic-api-key"
default_model = "claude-3-5-sonnet-20241022"

[gemini]
api_key = "your-google-api-key"
default_model = "gemini-2.5-flash"

Query Generation

SELECT generate_query('show all customers');
SELECT generate_query('monthly sales trend for the last year by category');
SELECT generate_query('customers who have not placed orders in the last 6 months');

Schema Discovery

SELECT get_database_tables();
SELECT get_table_details('orders');

Query Explanation

SELECT explain_query('SELECT * FROM users WHERE created_at > NOW() - INTERVAL ''7 days''');

Pass API credentials directly:

SELECT explain_query('SELECT * FROM products WHERE price > 100', 'your-api-key', 'anthropic');

Supported Models

  • OpenAI: gpt-4o, gpt-4o-mini
  • Anthropic: claude-3-5-sonnet-20241022, claude-3-haiku-20240307
  • Google Gemini: gemini-2.5-pro, gemini-2.5-flash

Last Modified 2026-03-12: add pg extension catalog (95749bf)