Skip to main content

Generate PostgreSQL queries from natural language using AI

Project description

PSQL Query Builder

Generate PostgreSQL queries from natural language using AI.

Overview

PSQL Query Builder is a Python tool that allows you to generate SQL queries for PostgreSQL databases using natural language. It leverages OpenAI's language models to translate your plain English requests into proper SQL queries, making database interaction more accessible.

Features

  • Generate SQL queries from natural language descriptions
  • Automatic database schema analysis with smart caching
  • Enhanced error handling with automatic fix suggestions
  • Execute generated queries and display results
  • Flexible configuration via environment variables or command-line arguments
  • Interactive mode for multiple queries
  • Single query mode for scripting

Installation

pip install psql-query-builder

Quick Start

Here's how to get started with PSQL Query Builder in just a few steps:

  1. Install the package:

    pip install psql-query-builder
    
  2. Set up your database connection (choose one):

    • Using command line: psql-query-builder --connection-string "postgresql://user:password@host:port/dbname"
    • Using environment variables: Copy .env.example to .env, edit with your details, then run psql-query-builder
  3. Ask questions in natural language:

    Enter your natural language query:
    > Show me all users who registered in the last month
    
  4. Get SQL and results: The tool will generate the SQL query and execute it, showing you the results.

Usage

Command Line Interface

The package provides a command-line interface with two modes of operation:

Interactive Mode

# Basic usage with connection string
psql-query-builder --connection-string "postgresql://user:password@host:port/dbname"

# Using individual connection parameters
psql-query-builder --host localhost --port 5432 --dbname mydb --user myuser --password mypassword

# Using environment variables (set PSQL_CONNECTION_STRING or individual PSQL_* variables)
psql-query-builder

Single Query Mode

# Run a single query and exit
psql-query-builder --query "Show me all users who signed up last month" --output-format json

# Generate SQL without executing it (dry run mode)
psql-query-builder --query "List all products with price greater than 100" --dry-run

Configuration Options

Environment Variables

A sample .env.example file is included in the package. You can copy this file to .env and update it with your values:

# Copy the example file to .env
cp .env.example .env

# Edit the .env file with your values
vim .env  # or use any text editor

Database Connection

You can configure the database connection in several ways (in order of precedence):

  1. Command-line arguments:

    --connection-string "postgresql://user:password@host:port/dbname"
    

    Or individual parameters:

    --host localhost --port 5432 --dbname mydb --user myuser --password mypassword --sslmode require
    
  2. Environment variables (in .env file or exported to your shell):

    PSQL_CONNECTION_STRING="postgresql://user:password@host:port/dbname"
    

    Or individual variables:

    PSQL_HOST=localhost
    PSQL_PORT=5432
    PSQL_DBNAME=mydb
    PSQL_USER=myuser
    PSQL_PASSWORD=mypassword
    PSQL_SSLMODE=require
    
  3. Interactive prompt if no connection details are provided

Schema Caching

The tool includes a smart schema caching system that significantly improves performance when running multiple queries against the same database. Key features include:

  • Automatic caching of database schema information
  • Configurable cache time-to-live (TTL)
  • Options to force refresh or clear the cache

To use schema caching options:

# Specify custom cache location
psql-query-builder --schema-cache-path ~/.cache/psql-query-builder

# Set custom TTL (in seconds, default is 24 hours)
psql-query-builder --schema-cache-ttl 3600

# Force refresh the schema cache
psql-query-builder --refresh-schema

# Clear the schema cache before running
psql-query-builder --clear-schema-cache

Schema caching is particularly useful for large databases where schema analysis can take significant time.

Enhanced Error Handling

The tool provides intelligent error handling with helpful suggestions when SQL queries fail:

  • Detailed error messages with context
  • Automatic suggestions for fixing common errors
  • Interactive fix application for quick recovery
  • Support for column and table name typos

When a query fails, the tool will:

  1. Analyze the error to determine the cause
  2. Suggest possible fixes based on the database schema
  3. Allow you to apply a fix and retry the query

This makes the tool much more user-friendly, especially for those who are not SQL experts.

Dry Run Mode

Dry run mode allows you to generate SQL queries without executing them. This is useful for:

  • Reviewing and validating generated SQL before execution
  • Learning how the tool translates natural language to SQL
  • Debugging or troubleshooting query generation
  • Saving queries for later execution

To use dry run mode, add the --dry-run or -d flag:

psql-query-builder --query "Find all transactions over $1000" --dry-run

The tool will connect to your database to analyze the schema, generate the SQL query using OpenAI, and then display the query without executing it. This is particularly helpful when working with production databases where you want to review queries before running them.

OpenAI API

Configure the OpenAI API:

  1. Command-line arguments:

    --openai-api-key "your-api-key"
    --model "gpt-4o-mini"
    --temperature 0.1
    
  2. Environment variables:

    OPENAI_API_KEY="your-api-key"
    
  3. Interactive prompt if API key is not provided

Python API

You can also use PSQL Query Builder as a library in your Python code:

from psql_query_builder import get_database_summary, generate_sql_prompt, generate_sql_with_openai, run_query

# Get database schema
connection_string = "postgresql://user:password@host:port/dbname"
db_schema = get_database_summary(connection_string)

# Generate SQL from natural language
user_query = "Show me all active users who registered in the last month"
prompt = generate_sql_prompt(user_query, db_schema)
sql_query = generate_sql_with_openai(prompt)

# Execute the query
results = run_query(connection_string, sql_query)
print(results)

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psql_query_builder-0.1.0.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

psql_query_builder-0.1.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file psql_query_builder-0.1.0.tar.gz.

File metadata

  • Download URL: psql_query_builder-0.1.0.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for psql_query_builder-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e1c02a5d4775b2ca36be4593a470a9b71c698ea4d831e3c689651a33549a875b
MD5 ae9fd655027561906c8a80bcaeacb007
BLAKE2b-256 98236893ba3768e60982b5499231e8a17cfe747cf574a56e4f201a5b2d007c34

See more details on using hashes here.

File details

Details for the file psql_query_builder-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for psql_query_builder-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 77865013d3facf355e8fada4937fbebc285511c2d75240e7868f023f6fdb6314
MD5 9094a34d1d4540991731da8f7020b19e
BLAKE2b-256 d99ee475b8c60223d093478ca711d9c3610fc3f86775714b503c66b841ed6a65

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page