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

..03-May-2022-

.github/workflows/H24-Jul-2021-

ci/H24-Jul-2021-

examples/H24-Jul-2021-

internal/sanitize/H24-Jul-2021-

log/H24-Jul-2021-

pgxpool/H24-Jul-2021-

stdlib/H24-Jul-2021-

.gitignoreH A D24-Jul-2021260

CHANGELOG.mdH A D24-Jul-20218.2 KiB

LICENSEH A D24-Jul-20211.1 KiB

README.mdH A D24-Jul-20219.2 KiB

batch.goH A D24-Jul-20214.5 KiB

batch_test.goH A D24-Jul-202118.2 KiB

bench_test.goH A D24-Jul-202133.2 KiB

conn.goH A D24-Jul-202125.1 KiB

conn_test.goH A D24-Jul-202130.2 KiB

copy_from.goH A D24-Jul-20215.1 KiB

copy_from_test.goH A D24-Jul-202114.7 KiB

doc.goH A D24-Jul-202111.4 KiB

example_custom_type_test.goH A D24-Jul-20212.4 KiB

example_json_test.goH A D24-Jul-2021617

extended_query_builder.goH A D24-Jul-20214 KiB

go.modH A D24-Jul-2021601

go.sumH A D24-Jul-202118.4 KiB

go_stdlib.goH A D24-Jul-20212.5 KiB

helper_test.goH A D24-Jul-20215.7 KiB

large_objects.goH A D24-Jul-20213.3 KiB

large_objects_test.goH A D24-Jul-20215.4 KiB

logger.goH A D24-Jul-20211.9 KiB

messages.goH A D24-Jul-2021402

pgbouncer_test.goH A D24-Jul-20212.2 KiB

query_test.goH A D24-Jul-202153.9 KiB

rows.goH A D24-Jul-20219.3 KiB

tx.goH A D24-Jul-202113.7 KiB

tx_test.goH A D24-Jul-202115 KiB

values.goH A D24-Jul-20216.8 KiB

values_test.goH A D24-Jul-202130.4 KiB

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	// urlExample := "postgres://username:password@localhost:5432/database_name"
33	conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
34	if err != nil {
35		fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
36		os.Exit(1)
37	}
38	defer conn.Close(context.Background())
39
40	var name string
41	var weight int64
42	err = conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
43	if err != nil {
44		fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
45		os.Exit(1)
46	}
47
48	fmt.Println(name, weight)
49}
50```
51
52See the [getting started guide](https://github.com/jackc/pgx/wiki/Getting-started-with-pgx) for more information.
53
54## Choosing Between the pgx and database/sql Interfaces
55
56It is recommended to use the pgx interface if:
571. The application only targets PostgreSQL.
582. No other libraries that require `database/sql` are in use.
59
60The pgx interface is faster and exposes more features.
61
62The `database/sql` interface only allows the underlying driver to return or receive the following types: `int64`,
63`float64`, `bool`, `[]byte`, `string`, `time.Time`, or `nil`. Handling other types requires implementing the
64`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.
65
66## Features
67
68pgx supports many features beyond what is available through `database/sql`:
69
70* Support for approximately 70 different PostgreSQL types
71* Automatic statement preparation and caching
72* Batch queries
73* Single-round trip query mode
74* Full TLS connection control
75* Binary format support for custom types (allows for much quicker encoding/decoding)
76* Copy protocol support for faster bulk data loads
77* 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)
78* Connection pool with after-connect hook for arbitrary connection setup
79* Listen / notify
80* Conversion of PostgreSQL arrays to Go slice mappings for integers, floats, and strings
81* Hstore support
82* JSON and JSONB support
83* Maps `inet` and `cidr` PostgreSQL types to `net.IPNet` and `net.IP`
84* Large object support
85* NULL mapping to Null* struct or pointer to pointer
86* Supports `database/sql.Scanner` and `database/sql/driver.Valuer` interfaces for custom types
87* Notice response handling
88* Simulated nested transactions with savepoints
89
90## Performance
91
92There are three areas in particular where pgx can provide a significant performance advantage over the standard
93`database/sql` interface and other drivers:
94
951. PostgreSQL specific types - Types such as arrays can be parsed much quicker because pgx uses the binary format.
962. Automatic statement preparation and caching - pgx will prepare and cache statements by default. This can provide an
97   significant free improvement to code that does not explicitly use prepared statements. Under certain workloads, it can
98   perform nearly 3x the number of queries per second.
993. Batched queries - Multiple queries can be batched together to minimize network round trips.
100
101## Comparison with Alternatives
102
103* [pq](http://godoc.org/github.com/lib/pq)
104* [go-pg](https://github.com/go-pg/pg)
105
106For 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.
107pgx also can perform better when using PostgreSQL-specific data types or query batching. See
108[go_db_bench](https://github.com/jackc/go_db_bench) for some database driver benchmarks.
109
110### Compatibility with `database/sql`
111
112pq is exclusively used with `database/sql`. go-pg does not use `database/sql` at all. pgx supports `database/sql` as well as
113its own interface.
114
115### Level of access, ORM
116
117go-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.
118
119pgx 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.
120
121## Testing
122
123pgx tests naturally require a PostgreSQL database. It will connect to the database specified in the `PGX_TEST_DATABASE` environment
124variable. The `PGX_TEST_DATABASE` environment variable can either be a URL or DSN. In addition, the standard `PG*` environment
125variables will be respected. Consider using [direnv](https://github.com/direnv/direnv) to simplify environment variable
126handling.
127
128### Example Test Environment
129
130Connect to your PostgreSQL server and run:
131
132```
133create database pgx_test;
134```
135
136Connect to the newly-created database and run:
137
138```
139create domain uint64 as numeric(20,0);
140```
141
142Now, you can run the tests:
143
144```
145PGX_TEST_DATABASE="host=/var/run/postgresql database=pgx_test" go test ./...
146```
147
148In addition, there are tests specific for PgBouncer that will be executed if `PGX_TEST_PGBOUNCER_CONN_STRING` is set.
149
150## Supported Go and PostgreSQL Versions
151
152pgx 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/).
153
154## Version Policy
155
156pgx follows semantic versioning for the documented public API on stable releases. `v4` is the latest stable major version.
157
158## PGX Family Libraries
159
160pgx is the head of a family of PostgreSQL libraries. Many of these can be used independently. Many can also be accessed
161from pgx for lower-level control.
162
163### [github.com/jackc/pgconn](https://github.com/jackc/pgconn)
164
165`pgconn` is a lower-level PostgreSQL database driver that operates at nearly the same level as the C library `libpq`.
166
167### [github.com/jackc/pgx/v4/pgxpool](https://github.com/jackc/pgx/tree/master/pgxpool)
168
169`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.
170
171### [github.com/jackc/pgx/v4/stdlib](https://github.com/jackc/pgx/tree/master/stdlib)
172
173This 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.
174
175### [github.com/jackc/pgtype](https://github.com/jackc/pgtype)
176
177Over 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.
178
179### [github.com/jackc/pgproto3](https://github.com/jackc/pgproto3)
180
181pgproto3 provides standalone encoding and decoding of the PostgreSQL v3 wire protocol. This is useful for implementing very low level PostgreSQL tooling.
182
183### [github.com/jackc/pglogrepl](https://github.com/jackc/pglogrepl)
184
185pglogrepl provides functionality to act as a client for PostgreSQL logical replication.
186
187### [github.com/jackc/pgmock](https://github.com/jackc/pgmock)
188
189pgmock 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).
190
191### [github.com/jackc/tern](https://github.com/jackc/tern)
192
193tern is a stand-alone SQL migration system.
194
195### [github.com/jackc/pgerrcode](https://github.com/jackc/pgerrcode)
196
197pgerrcode contains constants for the PostgreSQL error codes.
198
199## 3rd Party Libraries with PGX Support
200
201### [github.com/georgysavva/scany](https://github.com/georgysavva/scany)
202
203Library for scanning data from a database into Go structs and more.
204