Skip to content

pg_ai_query

AI-powered SQL query generation for PostgreSQL

pg_ai_query · AI-powered SQL query generation for PostgreSQL

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
2720pg_ai_querypg_ai_query0.1.1FEATApache-2.0C++
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-rNoYesNoYesyesno

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY0.1.119 18 17 16 15pg_ai_query-
RPMPIGSTY0.1.119 18 17 16 15pg_ai_query_$v-
DEBPIGSTY0.1.119 18 17 16 15postgresql-$v-ai-query-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/AN/AN/AN/AN/A
el8.aarch64N/AN/AN/AN/AN/A
el9.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
el9.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
el10.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
el10.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
d12.x86_64N/AN/AN/AN/AN/A
d12.aarch64N/AN/AN/AN/AN/A
d13.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
d13.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u22.x86_64N/AN/AN/AN/AN/A
u22.aarch64N/AN/AN/AN/AN/A
u24.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u24.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u26.x86_64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
u26.aarch64PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1PIGSTY 0.1.1
PackageVersionOSORGSIZEFile URL
pg_ai_query_190.1.1el9.x86_64pigsty886.7 KiBpg_ai_query_19-0.1.1-1PGSTY.el9.x86_64.rpm
pg_ai_query_190.1.1el9.aarch64pigsty802.4 KiBpg_ai_query_19-0.1.1-1PGSTY.el9.aarch64.rpm
pg_ai_query_190.1.1el10.x86_64pigsty890.0 KiBpg_ai_query_19-0.1.1-1PGSTY.el10.x86_64.rpm
pg_ai_query_190.1.1el10.aarch64pigsty834.3 KiBpg_ai_query_19-0.1.1-1PGSTY.el10.aarch64.rpm
postgresql-19-ai-query0.1.1d13.x86_64pigsty842.7 KiBpostgresql-19-ai-query_0.1.1-1PGSTY~trixie_amd64.deb
postgresql-19-ai-query0.1.1d13.aarch64pigsty759.3 KiBpostgresql-19-ai-query_0.1.1-1PGSTY~trixie_arm64.deb
postgresql-19-ai-query0.1.1u24.x86_64pigsty795.1 KiBpostgresql-19-ai-query_0.1.1-1PGSTY~noble_amd64.deb
postgresql-19-ai-query0.1.1u24.aarch64pigsty771.8 KiBpostgresql-19-ai-query_0.1.1-1PGSTY~noble_arm64.deb
postgresql-19-ai-query0.1.1u26.x86_64pigsty845.6 KiBpostgresql-19-ai-query_0.1.1-1PGSTY~resolute_amd64.deb
postgresql-19-ai-query0.1.1u26.aarch64pigsty874.1 KiBpostgresql-19-ai-query_0.1.1-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_ai_query_180.1.1el9.x86_64pigsty886.6 KiBpg_ai_query_18-0.1.1-1PGSTY.el9.x86_64.rpm
pg_ai_query_180.1.1el9.aarch64pigsty802.3 KiBpg_ai_query_18-0.1.1-1PGSTY.el9.aarch64.rpm
pg_ai_query_180.1.1el10.x86_64pigsty890.3 KiBpg_ai_query_18-0.1.1-1PGSTY.el10.x86_64.rpm
pg_ai_query_180.1.1el10.aarch64pigsty834.3 KiBpg_ai_query_18-0.1.1-1PGSTY.el10.aarch64.rpm
postgresql-18-ai-query0.1.1d13.x86_64pigsty844.7 KiBpostgresql-18-ai-query_0.1.1-1PGSTY~trixie_amd64.deb
postgresql-18-ai-query0.1.1d13.aarch64pigsty759.2 KiBpostgresql-18-ai-query_0.1.1-1PGSTY~trixie_arm64.deb
postgresql-18-ai-query0.1.1u24.x86_64pigsty795.1 KiBpostgresql-18-ai-query_0.1.1-1PGSTY~noble_amd64.deb
postgresql-18-ai-query0.1.1u24.aarch64pigsty771.8 KiBpostgresql-18-ai-query_0.1.1-1PGSTY~noble_arm64.deb
postgresql-18-ai-query0.1.1u26.x86_64pigsty845.6 KiBpostgresql-18-ai-query_0.1.1-1PGSTY~resolute_amd64.deb
postgresql-18-ai-query0.1.1u26.aarch64pigsty874.1 KiBpostgresql-18-ai-query_0.1.1-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_ai_query_170.1.1el9.x86_64pigsty887.3 KiBpg_ai_query_17-0.1.1-1PGSTY.el9.x86_64.rpm
pg_ai_query_170.1.1el9.aarch64pigsty802.1 KiBpg_ai_query_17-0.1.1-1PGSTY.el9.aarch64.rpm
pg_ai_query_170.1.1el10.x86_64pigsty890.1 KiBpg_ai_query_17-0.1.1-1PGSTY.el10.x86_64.rpm
pg_ai_query_170.1.1el10.aarch64pigsty834.2 KiBpg_ai_query_17-0.1.1-1PGSTY.el10.aarch64.rpm
postgresql-17-ai-query0.1.1d13.x86_64pigsty841.9 KiBpostgresql-17-ai-query_0.1.1-1PGSTY~trixie_amd64.deb
postgresql-17-ai-query0.1.1d13.aarch64pigsty760.1 KiBpostgresql-17-ai-query_0.1.1-1PGSTY~trixie_arm64.deb
postgresql-17-ai-query0.1.1u24.x86_64pigsty795.1 KiBpostgresql-17-ai-query_0.1.1-1PGSTY~noble_amd64.deb
postgresql-17-ai-query0.1.1u24.aarch64pigsty771.8 KiBpostgresql-17-ai-query_0.1.1-1PGSTY~noble_arm64.deb
postgresql-17-ai-query0.1.1u26.x86_64pigsty845.4 KiBpostgresql-17-ai-query_0.1.1-1PGSTY~resolute_amd64.deb
postgresql-17-ai-query0.1.1u26.aarch64pigsty874.2 KiBpostgresql-17-ai-query_0.1.1-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_ai_query_160.1.1el9.x86_64pigsty886.7 KiBpg_ai_query_16-0.1.1-1PGSTY.el9.x86_64.rpm
pg_ai_query_160.1.1el9.aarch64pigsty802.3 KiBpg_ai_query_16-0.1.1-1PGSTY.el9.aarch64.rpm
pg_ai_query_160.1.1el10.x86_64pigsty890.2 KiBpg_ai_query_16-0.1.1-1PGSTY.el10.x86_64.rpm
pg_ai_query_160.1.1el10.aarch64pigsty833.7 KiBpg_ai_query_16-0.1.1-1PGSTY.el10.aarch64.rpm
postgresql-16-ai-query0.1.1d13.x86_64pigsty841.6 KiBpostgresql-16-ai-query_0.1.1-1PGSTY~trixie_amd64.deb
postgresql-16-ai-query0.1.1d13.aarch64pigsty759.3 KiBpostgresql-16-ai-query_0.1.1-1PGSTY~trixie_arm64.deb
postgresql-16-ai-query0.1.1u24.x86_64pigsty795.7 KiBpostgresql-16-ai-query_0.1.1-1PGSTY~noble_amd64.deb
postgresql-16-ai-query0.1.1u24.aarch64pigsty771.8 KiBpostgresql-16-ai-query_0.1.1-1PGSTY~noble_arm64.deb
postgresql-16-ai-query0.1.1u26.x86_64pigsty846.5 KiBpostgresql-16-ai-query_0.1.1-1PGSTY~resolute_amd64.deb
postgresql-16-ai-query0.1.1u26.aarch64pigsty874.2 KiBpostgresql-16-ai-query_0.1.1-1PGSTY~resolute_arm64.deb
PackageVersionOSORGSIZEFile URL
pg_ai_query_150.1.1el9.x86_64pigsty887.3 KiBpg_ai_query_15-0.1.1-1PGSTY.el9.x86_64.rpm
pg_ai_query_150.1.1el9.aarch64pigsty803.0 KiBpg_ai_query_15-0.1.1-1PGSTY.el9.aarch64.rpm
pg_ai_query_150.1.1el10.x86_64pigsty889.5 KiBpg_ai_query_15-0.1.1-1PGSTY.el10.x86_64.rpm
pg_ai_query_150.1.1el10.aarch64pigsty833.8 KiBpg_ai_query_15-0.1.1-1PGSTY.el10.aarch64.rpm
postgresql-15-ai-query0.1.1d13.x86_64pigsty840.2 KiBpostgresql-15-ai-query_0.1.1-1PGSTY~trixie_amd64.deb
postgresql-15-ai-query0.1.1d13.aarch64pigsty759.5 KiBpostgresql-15-ai-query_0.1.1-1PGSTY~trixie_arm64.deb
postgresql-15-ai-query0.1.1u24.x86_64pigsty795.0 KiBpostgresql-15-ai-query_0.1.1-1PGSTY~noble_amd64.deb
postgresql-15-ai-query0.1.1u24.aarch64pigsty771.7 KiBpostgresql-15-ai-query_0.1.1-1PGSTY~noble_arm64.deb
postgresql-15-ai-query0.1.1u26.x86_64pigsty846.4 KiBpostgresql-15-ai-query_0.1.1-1PGSTY~resolute_amd64.deb
postgresql-15-ai-query0.1.1u26.aarch64pigsty874.2 KiBpostgresql-15-ai-query_0.1.1-1PGSTY~resolute_arm64.deb

Source

pig build pkg pg_ai_query;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

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

Install this extension with pig:

pig install pg_ai_query;		# install by package name

pig install pg_ai_query -v 19;   # install for PG 19
pig install pg_ai_query -v 18;   # install for PG 18
pig install pg_ai_query -v 17;   # install for PG 17
pig install pg_ai_query -v 16;   # install for PG 16
pig install pg_ai_query -v 15;   # install for PG 15

Create this extension with:

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

Table reader