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

Go
82%
AGPL 3.0

Lago

Ecommerce · Invoicing Finance

10,770

Open-source metering, billing, and revenue infrastructure for product-led companies that need complete control over their pricing stack.

View details
90
Repo Health
81
Technical
75
Dependency
Built with
Go 82%
Shell 11%
Updated 1 weeks ago
Go
97%
MIT

Formance Ledger

Databases · Developer Tools · Invoicing Finance

1,474

The programmable open source core ledger for fintech — build money-moving applications with atomic multi-posting transactions, account-based modeling, and Numscript, a built-in DSL for financial logic.

View details
91
Repo Health
8
Technical
69
Dependency
Built with
Go 97%
Updated 1 weeks ago
Go
55%
Other

Multica

AI Assistants · AI Development

55,176

Turn coding agents into real teammates — assign issues, track progress, and compound reusable skills across a vendor-neutral, self-hosted platform.

View details
87
Repo Health
83
Technical
67
Dependency
Built with
Go 55%
TypeScript 38%
Updated 1 weeks ago
Go
95%
Apache 2.0

Nakama

Developer Tools · Game Development

13,682

Open-source game backend server with built-in multiplayer, matchmaking, leaderboards, chat, authentication, and storage — deploy anywhere via Docker or binary.

View details
94
Repo Health
73
Technical
73
Dependency
Built with
Go 95%
Updated 1 weeks ago
Go
95%
Other

NetBird

Security

30,749

Replace your VPN with a zero-trust WireGuard overlay network that auto-connects devices, enforces SSO and posture checks, and deploys in under 5 minutes.

View details
92
Repo Health
82
Technical
66
Dependency
Built with
Go 95%
Updated 1 weeks ago
Go
48%
GPL 3.0

Netdata

Devops · Monitoring

81,296

Real-time per-second metrics, ML-powered anomaly detection, and zero-config observability for any infrastructure.

View details
93
Repo Health
86
Technical
70
Dependency
Built with
Go 48%
C 29%
Rust 14%
Updated 1 weeks ago
Go
76%
Apache 2.0

ntfy

Developer Tools · Marketing

35,351

Send push notifications to your phone or desktop from any script or service using a single HTTP PUT or POST—no sign-up required.

View details
89
Repo Health
89
Technical
76
Dependency
Built with
Go 76%
JavaScript 23%
Updated 2 weeks ago
Go
64%
MIT

Obot

AI Agents · Mcp

1,171

An open-source MCP platform for organizations — host MCP servers, run MCP registries, monitor usage, and build agents and chatbots on top of the Model Context Protocol from one self-hosted deployment.

View details
88
Repo Health
70
Technical
70
Dependency
Built with
Go 64%
Svelte 19%
TypeScript 15%
Updated 1 weeks ago
Go
79%
Apache 2.0

OpenMeter

Developer Tools · Invoicing Finance

2,439

Open-source metering and billing engine for AI, agentic, and DevTool monetization — ingest usage events in real time and turn them into accurate invoices automatically.

View details
87
Repo Health
81
Technical
66
Dependency
Built with
Go 79%
TypeScript 16%
Updated 1 weeks 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