pgx v4

A pure Go PostgreSQL driver and toolkit that goes beyond database/sql with binary-format performance and Postgres-specific features.

Library
Go
vv4.18.3
14,382 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
84 /100 Excellent
Development Activity 96
Maintenance 72
Community 68
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
80 /100 Excellent
Architecture 87
Code Quality 88
Innovation 68
Learning Curve 75

pgx is a pure Go driver and toolkit for PostgreSQL that aims to be low-level, fast, and performant while exposing PostgreSQL-specific features the standard database/sql package leaves out. This v4 release requires Go modules and is the previous stable major version, kept alive for existing projects that have not yet migrated to v5.

The driver can be used directly through its native interface for maximum speed, or through the bundled stdlib compatibility layer to work as a conventional database/sql driver while still allowing the native interface to be reclaimed for performance-sensitive code paths. Around 70 PostgreSQL types are supported, along with automatic statement preparation and caching, batch queries, full TLS control, LISTEN/NOTIFY, COPY protocol bulk loads, and large object support.

pgx v4 is the head of a small family of decoupled libraries: pgconn handles the low-level wire protocol, pgtype implements the PostgreSQL type system, and pgxpool (bundled in this repo) provides a concurrency-safe connection pool that sits entirely outside the core driver, so pgx can be used with a different pool implementation or none at all.

What You Get

  • A native Postgres driver (pgx.Connect, Query, Exec) that uses the binary wire format for faster encoding/decoding than text-based drivers
  • A database/sql-compatible layer (stdlib) so pgx can be dropped into existing code that expects the standard interface
  • pgxpool, a bundled concurrency-safe connection pool with an after-connect hook for per-connection setup
  • Support for roughly 70 PostgreSQL types, including arrays, hstore, JSON/JSONB, inet/cidr, and large objects
  • Batch query support and a single-round-trip query mode to cut network overhead
  • COPY protocol support for high-throughput bulk data loads
  • LISTEN/NOTIFY support and simulated nested transactions via savepoints
  • Pluggable logging adapters for log15, logrus, zap, and zerolog

Common Use Cases

  • Building a Postgres-backed Go service that needs more throughput than database/sql’s text-based scanning provides
  • Migrating an existing database/sql codebase onto pgx incrementally via the stdlib compatibility layer
  • Running high-volume bulk imports or exports through the COPY protocol instead of row-by-row inserts
  • Implementing pub/sub-style notifications inside Postgres using LISTEN/NOTIFY
  • Managing a pool of Postgres connections with custom per-connection setup via pgxpool’s after-connect hook
  • Working directly with advanced PostgreSQL types (arrays, hstore, JSON, network types) without hand-rolled marshaling

Under The Hood

Architecture

pgx v4 is organized as a thin core driver (conn.go, rows.go, tx.go, batch.go at the module root) layered directly on top of the lower-level pgconn and pgproto3 packages, which handle connection establishment and PostgreSQL’s binary wire protocol respectively; ConnConfig embeds pgconn.Config and is produced by ParseConfig before being handed to ConnectConfig, keeping connection setup and connection use as separate concerns. Type encoding/decoding is delegated entirely to the sibling pgtype package rather than implemented inline, and the connection pool (pgxpool/) and database/sql compatibility shim (stdlib/) are both separate, decoupled subpackages that consume the core pgx.Conn rather than being baked into it — so a caller can use the pool, the stdlib shim, both, or neither. This separation means the core driver has no opinion about pooling strategy, and swapping in a different pool implementation touches nothing in the driver itself.

Tech Stack

The module (module github.com/jackc/pgx/v4, Go 1.17) depends on a family of sibling jackc packages for protocol- and type-level work — pgconn, pgproto3/v2, pgtype, pgio, pgpassfile, pgservicefile, and the puddle pooling primitive used inside pgxpool — plus github.com/cockroachdb/apd for arbitrary-precision decimals and github.com/shopspring/decimal for a second decimal representation. Structured logging is pluggable via adapters over go-kit/log, sirupsen/logrus, uber-go/zap, rs/zerolog, and log15, none of which are hard dependencies of the core driver. There is no ORM or build tooling beyond the Go toolchain itself; deployment is simply go get, and CI (GitHub Actions) runs the suite against real Postgres versions 10 through 14 plus CockroachDB.

Code Quality

Testing is extensive and integration-first: root-level _test.go files (conn_test.go, query_test.go, values_test.go, tx_test.go, copy_from_test.go, batch_test.go) run against a live PostgreSQL instance rather than mocks, and CI’s matrix runs the full suite with the Go race detector (go test -race ./...) across two Go versions and five Postgres targets, including CockroachDB, giving real cross-version and cross-database compatibility coverage. Error handling favors explicit returned errors over panics throughout the public API, and the Rows result type is deliberately defined as an interface (rather than a concrete struct) specifically so tests can mock query behavior. Naming is consistent with Go conventions and the package is fully godoc-commented; there’s no separate linter config beyond go vet/gofmt conventions enforced by CI.

API Design pgx v4’s public surface mirrors database/sql’s Query/Scan idiom closely enough that a Go developer already familiar with the standard library can be productive immediately, while adding pgx-specific extensions (batch queries, COPY, LISTEN/NOTIFY) as opt-in additions rather than replacements. The doc.go package documentation walks through connection setup, pooling, and querying in order with runnable code samples, and the bundled examples/ directory (chat, todo, url_shortener) demonstrates realistic application usage rather than toy snippets, keeping the ramp-up cost low despite the driver’s larger feature set than database/sql.

Used by 41 apps in this directory

TypeScript
48%
AGPL 3.0

Grafana

Analytics · Monitoring

77,734

The open-source observability platform that unifies metrics, logs, and traces from any data source into dynamic, queryable dashboards.

View details
95
Repo Health
91
Technical
63
Dependency
Built with
TypeScript 48%
Go 47%
Updated 1 weeks ago
Go
69%
Other

Hanko

Authentication · Security

9,065

Open source, self-hostable authentication platform with passkeys, SAML SSO, and OAuth — the privacy-first alternative to Auth0 and Clerk.

View details
91
Repo Health
81
Technical
65
Dependency
Built with
Go 69%
TypeScript 27%
Updated 1 weeks ago
Go
62%
Apache 2.0

Harness Open Source

Code Editors · Developer Tools · Devops

38,849

A unified open source DevOps platform combining Git hosting, CI/CD pipelines, cloud development environments, and artifact registries in a single self-hosted system.

View details
90
Repo Health
79
Technical
64
Dependency
Built with
Go 62%
TypeScript 33%
Updated 1 weeks ago
Go
87%
MIT

Hatchet

AI Development · Automation · Developer Tools

8,236

A Postgres-backed orchestration engine for background tasks, AI agents, and durable workflows that replaces Redis queues and multi-datastore durable execution platforms with a single self-hostable service.

View details
88
Repo Health
83
Technical
67
Dependency
Built with
Go 87%
Updated 1 weeks ago
TypeScript
71%
Other

highlight.io

Analytics · Developer Tools · Monitoring

9,386

Open-source full-stack monitoring that unifies session replay, error tracking, logging, and distributed tracing so you can stop context-switching between tools.

View details
74
Repo Health
78
Technical
65
Dependency
Built with
TypeScript 71%
Go 16%
Updated 3 weeks ago
Go
67%
MIT

hoop

Monitoring · Security

844

A wire-protocol gateway that enforces data masking, command blocking, approval workflows, and full session recording for engineers and AI agents accessing production infrastructure.

View details
86
Repo Health
80
Technical
64
Dependency
Built with
Go 67%
Clojure 12%
JavaScript 12%
Updated 1 weeks ago
TypeScript
93%
Other

Infisical

Devops · Security

30,320

The open-source platform for secrets, certificates, privileged access, and AI agent security — all in one self-hostable system.

View details
91
Repo Health
84
Technical
64
Dependency
Built with
TypeScript 93%
Updated 1 weeks ago
TypeScript
67%
MIT

Jitsu

Data Engineering

5,131

Open-source, fully-scriptable data ingestion engine that streams events from web, apps, and APIs to any data warehouse in real time.

View details
90
Repo Health
79
Technical
66
Dependency
Built with
TypeScript 67%
Go 31%
Updated 1 weeks ago
TypeScript
44%
MIT

Kuku

Note Taking

232

A local-first, open-source Markdown knowledge workspace for macOS — plain files, personal wiki and Second Brain workflows, AI-assisted diffs, and encrypted sync, built as an Obsidian alternative.

View details
75
Repo Health
67
Technical
67
Dependency
Built with
TypeScript 44%
Rust 41%
Updated 1 months ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers