• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/workflows/H25-Mar-2021-5242

ci/H25-Mar-2021-5044

examples/H25-Mar-2021-433329

internal/sanitize/H25-Mar-2021-511460

log/H25-Mar-2021-253196

pgxpool/H25-Mar-2021-2,2251,636

stdlib/H25-Mar-2021-2,0201,599

.gitignoreH A D25-Mar-2021260 2518

CHANGELOG.mdH A D25-Mar-20217.6 KiB194142

LICENSEH A D25-Mar-20211.1 KiB2318

README.mdH A D25-Mar-20219.2 KiB203133

batch.goH A D25-Mar-20214.5 KiB180132

batch_test.goH A D25-Mar-202118.2 KiB832645

bench_test.goH A D25-Mar-202133.2 KiB1,3971,152

conn.goH A D25-Mar-202125.1 KiB851653

conn_test.goH A D25-Mar-202129.7 KiB1,064821

copy_from.goH A D25-Mar-20215.1 KiB212154

copy_from_test.goH A D25-Mar-202114.7 KiB607482

doc.goH A D25-Mar-202111.4 KiB3411

example_custom_type_test.goH A D25-Mar-20212.4 KiB11689

example_json_test.goH A D25-Mar-2021617 4029

extended_query_builder.goH A D25-Mar-20214 KiB169135

go.modH A D25-Mar-2021629 2219

go.sumH A D25-Mar-202146.8 KiB485484

go_stdlib.goH A D25-Mar-20212.5 KiB6214

helper_test.goH A D25-Mar-20215.7 KiB174141

large_objects.goH A D25-Mar-20213.3 KiB12281

large_objects_test.goH A D25-Mar-20215.4 KiB303244

logger.goH A D25-Mar-20211.9 KiB9974

messages.goH A D25-Mar-2021402 2420

pgbouncer_test.goH A D25-Mar-20212.2 KiB8166

query_test.goH A D25-Mar-202153.9 KiB2,1081,689

rows.goH A D25-Mar-20219.3 KiB348251

tx.goH A D25-Mar-202113.6 KiB440287

tx_test.goH A D25-Mar-202115 KiB622487

values.goH A D25-Mar-20216.7 KiB279242

values_test.goH A D25-Mar-202130.4 KiB1,066912

README.md

1[![](https://godoc.org/github.com/jackc/pgx?status.svg)](https://pkg.go.dev/github.com/jackc/pgx/v4)
2[![Build Status](https://travis-ci.org/jackc/pgx.svg)](https://travis-ci.org/jackc/pgx)
3
4# pgx - PostgreSQL Driver and Toolkit
5
6pgx is a pure Go driver and toolkit for PostgreSQL.
7
8pgx aims to be low-level, fast, and performant, while also enabling PostgreSQL-specific features that the standard `database/sql` package does not allow for.
9
10The driver component of pgx can be used alongside the standard `database/sql` package.
11
12The toolkit component is a related set of packages that implement PostgreSQL functionality such as parsing the wire protocol
13and type mapping between PostgreSQL and Go. These underlying packages can be used to implement alternative drivers,
14proxies, load balancers, logical replication clients, etc.
15
16The current release of `pgx v4` requires Go modules. To use the previous version, checkout and vendor the `v3` branch.
17
18## Example Usage
19
20```go
21package main
22
23import (
24	"context"
25	"fmt"
26	"os"
27
28	"github.com/jackc/pgx/v4"
29)
30
31func main() {
32	conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
33	if err != nil {
34		fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
35		os.Exit(1)
36	}
37	defer conn.Close(context.Background())
38
39	var name string
40	var weight int64
41	err = conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
42	if err != nil {
43		fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
44		os.Exit(1)
45	}
46
47	fmt.Println(name, weight)
48}
49```
50
51See the [getting started guide](https://github.com/jackc/pgx/wiki/Getting-started-with-pgx) for more information.
52
53## Choosing Between the pgx and database/sql Interfaces
54
55It is recommended to use the pgx interface if:
561. The application only targets PostgreSQL.
572. No other libraries that require `database/sql` are in use.
58
59The pgx interface is faster and exposes more features.
60
61The `database/sql` interface only allows the underlying driver to return or receive the following types: `int64`,
62`float64`, `bool`, `[]byte`, `string`, `time.Time`, or `nil`. Handling other types requires implementing the
63`database/sql.Scanner` and the `database/sql/driver/driver.Valuer` interfaces which require transmission of values in text format. The binary format can be substantially faster, which is what the pgx interface uses.
64
65## Features
66
67pgx supports many features beyond what is available through `database/sql`:
68
69* Support for approximately 70 different PostgreSQL types
70* Automatic statement preparation and caching
71* Batch queries
72* Single-round trip query mode
73* Full TLS connection control
74* Binary format support for custom types (allows for much quicker encoding/decoding)
75* Copy protocol support for faster bulk data loads
76* Extendable logging support including built-in support for `log15adapter`, [`logrus`](https://github.com/sirupsen/logrus), [`zap`](https://github.com/uber-go/zap), and [`zerolog`](https://github.com/rs/zerolog)
77* Connection pool with after-connect hook for arbitrary connection setup
78* Listen / notify
79* Conversion of PostgreSQL arrays to Go slice mappings for integers, floats, and strings
80* Hstore support
81* JSON and JSONB support
82* Maps `inet` and `cidr` PostgreSQL types to `net.IPNet` and `net.IP`
83* Large object support
84* NULL mapping to Null* struct or pointer to pointer
85* Supports `database/sql.Scanner` and `database/sql/driver.Valuer` interfaces for custom types
86* Notice response handling
87* Simulated nested transactions with savepoints
88
89## Performance
90
91There are three areas in particular where pgx can provide a significant performance advantage over the standard
92`database/sql` interface and other drivers:
93
941. PostgreSQL specific types - Types such as arrays can be parsed much quicker because pgx uses the binary format.
952. Automatic statement preparation and caching - pgx will prepare and cache statements by default. This can provide an
96   significant free improvement to code that does not explicitly use prepared statements. Under certain workloads, it can
97   perform nearly 3x the number of queries per second.
983. Batched queries - Multiple queries can be batched together to minimize network round trips.
99
100## Comparison with Alternatives
101
102* [pq](http://godoc.org/github.com/lib/pq)
103* [go-pg](https://github.com/go-pg/pg)
104
105For prepared queries with small sets of simple data types, all drivers will have have similar performance. However, if prepared statements aren't being explicitly used, pgx can have a significant performance advantage due to automatic statement preparation.
106pgx also can perform better when using PostgreSQL-specific data types or query batching. See
107[go_db_bench](https://github.com/jackc/go_db_bench) for some database driver benchmarks.
108
109### Compatibility with `database/sql`
110
111pq is exclusively used with `database/sql`. go-pg does not use `database/sql` at all. pgx supports `database/sql` as well as
112its own interface.
113
114### Level of access, ORM
115
116go-pg is a PostgreSQL client and ORM. It includes many features that traditionally sit above the database driver, such as ORM, struct mapping, soft deletes, schema migrations, and sharding support.
117
118pgx is "closer to the metal" and such abstractions are beyond the scope of the pgx project, which first and foremost, aims to be a performant driver and toolkit.
119
120## Testing
121
122pgx tests naturally require a PostgreSQL database. It will connect to the database specified in the `PGX_TEST_DATABASE` environment
123variable. The `PGX_TEST_DATABASE` environment variable can either be a URL or DSN. In addition, the standard `PG*` environment
124variables will be respected. Consider using [direnv](https://github.com/direnv/direnv) to simplify environment variable
125handling.
126
127### Example Test Environment
128
129Connect to your PostgreSQL server and run:
130
131```
132create database pgx_test;
133```
134
135Connect to the newly-created database and run:
136
137```
138create domain uint64 as numeric(20,0);
139```
140
141Now, you can run the tests:
142
143```
144PGX_TEST_DATABASE="host=/var/run/postgresql database=pgx_test" go test ./...
145```
146
147In addition, there are tests specific for PgBouncer that will be executed if `PGX_TEST_PGBOUNCER_CONN_STRING` is set.
148
149## Supported Go and PostgreSQL Versions
150
151pgx supports the same versions of Go and PostgreSQL that are supported by their respective teams. For [Go](https://golang.org/doc/devel/release.html#policy) that is the two most recent major releases and for [PostgreSQL](https://www.postgresql.org/support/versioning/) the major releases in the last 5 years. This means pgx supports Go 1.15 and higher and PostgreSQL 9.6 and higher. pgx also is tested against the latest version of [CockroachDB](https://www.cockroachlabs.com/product/).
152
153## Version Policy
154
155pgx follows semantic versioning for the documented public API on stable releases. `v4` is the latest stable major version.
156
157## PGX Family Libraries
158
159pgx is the head of a family of PostgreSQL libraries. Many of these can be used independently. Many can also be accessed
160from pgx for lower-level control.
161
162### [github.com/jackc/pgconn](https://github.com/jackc/pgconn)
163
164`pgconn` is a lower-level PostgreSQL database driver that operates at nearly the same level as the C library `libpq`.
165
166### [github.com/jackc/pgx/v4/pgxpool](https://github.com/jackc/pgx/tree/master/pgxpool)
167
168`pgxpool` is a connection pool for pgx. pgx is entirely decoupled from its default pool implementation. This means that pgx can be used with a different pool or without any pool at all.
169
170### [github.com/jackc/pgx/v4/stdlib](https://github.com/jackc/pgx/tree/master/stdlib)
171
172This is a `database/sql` compatibility layer for pgx. pgx can be used as a normal `database/sql` driver, but at any time, the native interface can be acquired for more performance or PostgreSQL specific functionality.
173
174### [github.com/jackc/pgtype](https://github.com/jackc/pgtype)
175
176Over 70 PostgreSQL types are supported including `uuid`, `hstore`, `json`, `bytea`, `numeric`, `interval`, `inet`, and arrays. These types support `database/sql` interfaces and are usable outside of pgx. They are fully tested in pgx and pq. They also support a higher performance interface when used with the pgx driver.
177
178### [github.com/jackc/pgproto3](https://github.com/jackc/pgproto3)
179
180pgproto3 provides standalone encoding and decoding of the PostgreSQL v3 wire protocol. This is useful for implementing very low level PostgreSQL tooling.
181
182### [github.com/jackc/pglogrepl](https://github.com/jackc/pglogrepl)
183
184pglogrepl provides functionality to act as a client for PostgreSQL logical replication.
185
186### [github.com/jackc/pgmock](https://github.com/jackc/pgmock)
187
188pgmock offers the ability to create a server that mocks the PostgreSQL wire protocol. This is used internally to test pgx by purposely inducing unusual errors. pgproto3 and pgmock together provide most of the foundational tooling required to implement a PostgreSQL proxy or MitM (such as for a custom connection pooler).
189
190### [github.com/jackc/tern](https://github.com/jackc/tern)
191
192tern is a stand-alone SQL migration system.
193
194### [github.com/jackc/pgerrcode](https://github.com/jackc/pgerrcode)
195
196pgerrcode contains constants for the PostgreSQL error codes.
197
198## 3rd Party Libraries with PGX Support
199
200### [github.com/georgysavva/scany](https://github.com/georgysavva/scany)
201
202Library for scanning data from a database into Go structs and more.
203