Skip to content

pgx

A Go library for connecting to and querying PostgreSQL databases.

Stars
14.4k
Forks
1.2k
Licence
MIT
Language
Go
Last update
6 days ago
Type
Library

What it is

pgx is a pure Go driver and toolkit for PostgreSQL. It lets Go programs connect to a PostgreSQL database, run queries, and use PostgreSQL-specific features such as LISTEN/NOTIFY and COPY. It also includes an adapter for Go's standard database/sql interface, and lower-level packages for working with the PostgreSQL wire protocol and type mapping.

  • Connect a Go web service to PostgreSQL using a DATABASE_URL environment variable
  • Run queries and scan results into Go variables
  • Bulk-load data using the COPY protocol
  • Listen for database notifications with LISTEN/NOTIFY
  • Pool connections for concurrent use with pgxpool

Great for

  • Go developers building services that only use PostgreSQL
  • Teams that need PostgreSQL-specific features like LISTEN/NOTIFY or COPY
  • Projects that want a built-in connection pool (pgxpool)
  • Developers building proxies, load balancers, or replication clients on the wire protocol

Not ideal for

  • Projects that need to support multiple databases through database/sql, because pgx's native interface is PostgreSQL-only
  • Non-Go projects, since this is a Go library
  • Beginners with no Go experience, since the README assumes familiarity with Go and database APIs
Step-by-step guide: how to use pgxInstall, first run, examples, tips and FAQs

How to get it

  1. 1

    Install with a package manager

    You'll need: Go 1.25 or higher · PostgreSQL 14 or higher (for running the database)

    Install the library

    go get github.com/jackc/pgx/v5
  2. 2

    Run it

    • Install the module with go get github.com/jackc/pgx/v5.
    • Set the DATABASE_URL environment variable to a PostgreSQL connection string.
    • Copy the example program from the README, which calls pgx.Connect and runs a QueryRow.
    • Run the program with go run and check the printed output.
  3. 3

    Get the most out of it

    • Use pgxpool for concurrency-safe connection pooling in servers
    • Use Batch queries and single-round trip query mode to reduce network round trips
    • Use COPY for faster bulk data loads
    • Hook up tracing or logging via the tracelog package and the listed adapters for zap, zerolog, slog, and others
    • Use the PGX family libraries such as pglogrepl and tern for replication and migrations

Downloads

Strengths

  • Exposes PostgreSQL-specific features such as LISTEN/NOTIFY and COPY
  • Includes a database/sql compatibility adapter, so existing code can adopt it gradually
  • Documented on pkg.go.dev with separate packages for the base driver, pool, and adapter

Trade-offs

  • Its native interface is PostgreSQL-only, so it is not a drop-in choice for multi-database code
  • Requires Go 1.25+ and PostgreSQL 14+ per the README's support policy
  • Using the database/sql adapter gives up some of the faster native interface

How it compares

ProjectStarsTypeLevelUpdatedBest for
pgx (this)14.4kLibraryintermediate6 days agoGo developers building services that only use PostgreSQL
gorm40kLibraryintermediate13 days agoGo developers building web services or APIs that store data in a relational database
sqlc18.4kDev toolintermediate4 days agoGo developers who prefer writing plain SQL over using an ORM

Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.