API Reference

pgx

PostgreSQL driver and toolkit for Go with connection pooling, prepared statements and native types.

Official Documentation ↗
CompatibilityThis documentation covers version pgx v5 / PostgreSQL 12+ / Go 1.20+. Check the official docs for the latest API surface.

Installation

gogo get github.com/jackc/pgx/v5

Methods

Overview

pgx is a PostgreSQL driver and toolkit for Go. It provides a native connection API, a connection pool, and a database/sql compatible driver, with strong support for PostgreSQL types.

Core Methods

Connect or create a pool, then use Query, QueryRow and Exec with parameters. Scan maps columns to Go values, and transactions use Begin and Commit or the pool transaction helper.

Usage Notes

Use a pool sized to the database limits, pass parameters with placeholders, and always close rows. Prefer the native interface for performance and the database/sql driver for compatibility.

Common Pitfalls

Forgetting rows.Close leaks connections. Using more pool connections than the database allows causes wait timeouts. Ignoring context cancellation leaves queries running after the caller has gone.

Errors

The following points are quoted verbatim from the official documentation (https://github.com/jackc/pgx). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

When should I use pgxpool?
Use pgxpool in servers and any long-running process so connections are reused and concurrency is bounded. A bare pgx.Connect suits scripts and short-lived tools.
How does pgx compare to database/sql?
pgx offers a native interface with better performance and PostgreSQL-specific features, and also provides a database/sql compatible driver when you need the standard interface.
How do I scan types like jsonb or arrays?
pgx maps many PostgreSQL types to Go values directly and also supports pgtype wrappers. For custom types register a codec so scanning and encoding work as expected.