https://github.com/lib/pq
Go PostgreSQL driver for database/sql
https://github.com/lib/pq
Last synced: about 1 month ago
JSON representation
Go PostgreSQL driver for database/sql
- Host: GitHub
- URL: https://github.com/lib/pq
- Owner: lib
- License: mit
- Created: 2012-03-12T18:50:22.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2026-01-18T19:41:00.000Z (about 1 month ago)
- Last Synced: 2026-01-19T03:13:42.903Z (about 1 month ago)
- Language: Go
- Homepage: https://pkg.go.dev/github.com/lib/pq
- Size: 1.18 MB
- Stars: 9,760
- Watchers: 145
- Forks: 934
- Open Issues: 119
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-cn - pq
- go-awesome - pq - PostgreSQL 驱动 (开源类库 / 数据库)
- awesome-go-storage - pq - Pure Go Postgres driver for database/sql. (Database Drivers)
- awesome-go - pq - Pure Go Postgres driver for database/sql. Stars:`9.6K`. (Database Drivers / Relational Database Drivers)
- awesome-go-cn - pq
- go-awesome - pq - PostgreSQL driver (Open source library / Database)
- awesome-go - pq - Pure Go Postgres driver for database/sql - ★ 4345 (Database Drivers)
- awesome-go - pq - Pure Go Postgres driver for database/sql. (Database Drivers / Relational Database Drivers)
- starred-awesome - pq - Pure Go Postgres driver for database/sql (Go)
- awesome-go-storage - pq - Pure Go Postgres driver for database/sql. (Database Drivers)
- awesome-cobol - pq - Pure Cobol Postgres driver for database/sql. (Database Drivers / Middlewares)
- awesome-go - pq - 数据库/sql的Pure Go Postgres驱动程序。 (<span id="数据库驱动-database-drivers">数据库驱动 Database Drivers</span> / <span id="高级控制台用户界面-advanced-console-uis">高级控制台用户界面 Advanced Console UIs</span>)
- awesome-sql - lib/pq
- awesome-go-extra - pq - 03-12T18:50:22Z|2022-08-20T17:36:41Z| (Generators / Relational Database Drivers)
- awesome-go-info - pq
- awesome-go - pq - Pure Go Postgres driver for database/sql. - :arrow_down:5873 - :star:2324 (Database Drivers / Advanced Console UIs)
- awesome-Char - pq - Pure Go Postgres driver for database/sql. (Database Drivers / Advanced Console UIs)
- awesome-go - pq - Pure Go Postgres driver for database/sql. (Database Drivers / Relational Database Drivers)
- fucking-awesome-go - pq - Pure Go Postgres driver for database/sql. (Database Drivers / Relational Database Drivers)
- awesome-go - pq - Pure Go Postgres driver for database/sql. (Database Drivers / Advanced Console UIs)
- go-awesome-with-star-updatetime - pq - Pure Go Postgres driver for database/sql. (Database Drivers / Advanced Console UIs)
- fucking-awesome-go - :octocat: pq - Pure Go Postgres driver for database/sql. :star: 2111 :fork_and_knife: 319 (Database Drivers / Advanced Console UIs)
- awesome-go-with-stars - pq - 02-18 | (Data Integration Frameworks / Relational Database Drivers)
- awesome-ccamel - lib/pq - Go PostgreSQL driver for database/sql (Go)
- awesome-go - pq - Pure Go Postgres driver for database/sql. (Database Drivers / Relational Database Drivers)
- awesome-pg - GitHub - lib/pq: Pure Go Postgres driver for database/sql
- awesome-go-processed - pq - Pure Go Postgres driver for database/sql.| (Database Drivers / Advanced Console UIs)
- my-awesome - lib/pq - 02 star:9.8k fork:0.9k Go PostgreSQL driver for database/sql (Go)
- awesome-go - pq - Pure Go Postgres driver for database/sql. (Database Drivers / Advanced Console UIs)
- awesome-go-cn - pq
- awesome-go-cn - pq
- awesome - lib/pq - Go PostgreSQL driver for database/sql (Go)
- awesome-go - pq - Pure Go Postgres driver for database/sql. (Database Drivers / Relational Database Drivers)
- awesome-go - pq - Pure Go Postgres driver for database/sql. (Database Drivers / Relational Database Drivers)
- awesome-starts - lib/pq - Pure Go Postgres driver for database/sql (Go)
- awesome-go - pq - | - | - | (Database Drivers / Advanced Console UIs)
- awesome-go-plus - pq - Pure Go Postgres driver for database/sql.  (Database Drivers / Relational Database Drivers)
- awesome-postgres-with-stars - pq - 01-29 | (Language bindings)
- awesome-go - pq - Pure Go Postgres driver for database/sql. (Database Drivers / Relational Database Drivers)
README
pq is a Go PostgreSQL driver for database/sql.
All [maintained versions of PostgreSQL] are supported. Older versions may work,
but this is not tested.
API docs: https://pkg.go.dev/github.com/lib/pq
Install with:
go get github.com/lib/pq@latest
[maintained versions of PostgreSQL]: https://www.postgresql.org/support/versioning
Features
--------
* SSL
* Handles bad connections for `database/sql`
* Scan `time.Time` correctly (i.e. `timestamp[tz]`, `time[tz]`, `date`)
* Scan binary blobs correctly (i.e. `bytea`)
* Package for `hstore` support
* COPY FROM support
* pq.ParseURL for converting urls to connection strings for sql.Open.
* Many libpq compatible environment variables
* Unix socket support
* Notifications: `LISTEN`/`NOTIFY`
* pgpass support
* GSS (Kerberos) auth
Running Tests
-------------
Tests need to be run against a PostgreSQL database; you can use Docker compose
to start one:
docker compose up -d
This starts the latest PostgreSQL; use `docker compose up -d pg«v»` to start a
different version.
In addition, your `/etc/hosts` currently needs an entry:
127.0.0.1 postgres postgres-invalid
Or you can use any other PostgreSQL instance; see
`testdata/init/docker-entrypoint-initdb.d` for the required setup. You can use
the standard `PG*` environment variables to control the connection details; it
uses the following defaults:
PGHOST=localhost
PGDATABASE=pqgo
PGUSER=pqgo
PGSSLMODE=disable
PGCONNECT_TIMEOUT=20
`PQTEST_BINARY_PARAMETERS` can be used to add `binary_parameters=yes` to all
connection strings:
PQTEST_BINARY_PARAMETERS=1 go test
Tests can be run against pgbouncer with:
docker compose up -d pgbouncer pg18
PGPORT=6432 go test ./...
and pgpool with:
docker compose up -d pgpool pg18
PGPORT=7432 go test ./...
You can use PQGO_DEBUG=1 to make the driver print the communication with
PostgreSQL to stderr; this works anywhere (test or applications) and can be
useful to debug protocol problems.
For example:
% PQGO_DEBUG=1 go test -run TestSimpleQuery
CLIENT → Startup 69 "\x00\x03\x00\x00database\x00pqgo\x00user [..]"
SERVER ← (R) AuthRequest 4 "\x00\x00\x00\x00"
SERVER ← (S) ParamStatus 19 "in_hot_standby\x00off\x00"
[..]
SERVER ← (Z) ReadyForQuery 1 "I"
START conn.query
START conn.simpleQuery
CLIENT → (Q) Query 9 "select 1\x00"
SERVER ← (T) RowDescription 29 "\x00\x01?column?\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x17\x00\x04\xff\xff\xff\xff\x00\x00"
SERVER ← (D) DataRow 7 "\x00\x01\x00\x00\x00\x011"
END conn.simpleQuery
END conn.query
SERVER ← (C) CommandComplete 9 "SELECT 1\x00"
SERVER ← (Z) ReadyForQuery 1 "I"
CLIENT → (X) Terminate 0 ""
PASS
ok github.com/lib/pq 0.010s