PostgreSQL driver and toolkit for Go
 
 
 
Go to file
Evan Jones 11d892dfcf pgconn.CancelRequest: Fix unix sockets: don't use RemoteAddr()
The tests for cancelling requests were failing when using unix
sockets. The reason is that net.Conn.RemoteAddr() calls getpeername()
to get the address. For Unix sockets, this returns the address that
was passed to bind() by the *server* process, not the address that
was passed to connect() by the *client*. For postgres, this is always
relative to the server's directory, so is a path like:

    ./.s.PGSQL.5432

Since it does not return the full absolute path, this function cannot
connect, so it cannot cancel requests. To fix it, use the connection's
config for Unix sockets. I think this should be okay, since a system
using unix sockets should not have "fallbacks". If that is incorrect,
we will need to save the address on PgConn.

Fixes the following failed tests when using Unix sockets:

--- FAIL: TestConnCancelRequest (2.00s)
    pgconn_test.go:2056:
          Error Trace:  /Users/evan.jones/pgx/pgconn/pgconn_test.go:2056
                              /Users/evan.jones/pgx/pgconn/asm_arm64.s:1172
          Error:        Received unexpected error:
                        dial unix ./.s.PGSQL.5432: connect: no such file or directory
          Test:         TestConnCancelRequest
    pgconn_test.go:2063:
          Error Trace:  /Users/evan.jones/pgx/pgconn/pgconn_test.go:2063
          Error:        Object expected to be of type *pgconn.PgError, but was <nil>
          Test:         TestConnCancelRequest
--- FAIL: TestConnContextCanceledCancelsRunningQueryOnServer (5.10s)
    pgconn_test.go:2109:
          Error Trace:  /Users/evan.jones/pgx/pgconn/pgconn_test.go:2109
          Error:        Received unexpected error:
                        timeout: context already done: context deadline exceeded
          Test:         TestConnContextCanceledCancelsRunningQueryOnServer
2023-05-20 08:08:47 -05:00
.github CI fix: Go versions are strings 2023-02-04 07:32:13 -06:00
ci Partial CI fixes 2022-10-31 21:24:57 -05:00
examples Rename pgxpool.NewConfig to NewWithConfig 2022-07-30 09:16:42 -05:00
internal Use `time.Equal` instead of direct comparison 2023-03-24 17:51:34 -05:00
log/testingadapter Remove pgx logging code moved to tracelog 2022-07-23 08:54:59 -05:00
pgconn pgconn.CancelRequest: Fix unix sockets: don't use RemoteAddr() 2023-05-20 08:08:47 -05:00
pgproto3 Fix tests for iobufpool optimization 2023-01-28 09:30:12 -06:00
pgtype pgtype.Hstore: Fix quoting of whitespace; Add test 2023-05-16 07:02:55 -05:00
pgxpool run tests that rely on backend PID to run against cockroach 2023-05-15 18:06:08 -05:00
pgxtest Skipped multirange tests for postgres less than 14 version 2022-12-01 19:33:33 -06:00
stdlib simplify duplicate `pgx` registration guard 2023-02-13 21:08:42 -06:00
testsetup Contributing guide now includes instructions to test client ssl auth 2022-10-29 19:00:29 -05:00
tracelog Use DefaultQueryExecMode in CopyFrom 2022-12-23 13:22:26 -06:00
.gitignore Use DefaultQueryExecMode in CopyFrom 2022-12-23 13:22:26 -06:00
CHANGELOG.md Release v5.3.1 2023-02-27 19:57:26 -06:00
CONTRIBUTING.md Update CONTRIBUTING.md 2022-11-12 10:42:02 -06:00
LICENSE Update copyright date 2021-03-25 10:08:09 -04:00
README.md Add KSQL on the 3rd party section of the README 2023-04-11 17:53:38 -05:00
Rakefile Add Array and FlatArray container types 2022-04-16 11:33:45 -05:00
batch.go Fix error when using BatchResults.Exec 2023-04-20 21:43:59 -05:00
batch_test.go Fix error when using BatchResults.Exec 2023-04-20 21:43:59 -05:00
bench_test.go func multiInsert returns nil when err != nil 2023-01-16 20:06:01 -06:00
conn.go pgx.Conn: Fix memory leak: Delete items from preparedStatements 2023-05-20 08:06:37 -05:00
conn_internal_test.go pgx.Conn: Fix memory leak: Delete items from preparedStatements 2023-05-20 08:06:37 -05:00
conn_test.go Remove sleeps in test 2023-02-27 20:32:51 -06:00
copy_from.go Set socket to non-blocking mode in `Read`, `Flush` and `BufferReadUntilBlock` operations 2023-03-24 17:51:34 -05:00
copy_from_test.go Use DefaultQueryExecMode in CopyFrom 2022-12-23 13:22:26 -06:00
doc.go Unregistered OIDs are handled the same as unknown OIDs 2022-12-23 13:14:56 -06:00
extended_query_builder.go Fix encode to json ignoring driver.Valuer 2022-12-23 13:44:09 -06:00
go.mod Try to make windows non-blocking I/O 2023-03-04 09:25:36 -06:00
go.sum Try to make windows non-blocking I/O 2023-03-04 09:25:36 -06:00
helper_test.go Fix some bad rows.Err() handlings in tests 2022-10-14 19:02:44 -05:00
large_objects.go Go 1.19 go fmt 2022-08-06 06:20:50 -05:00
large_objects_test.go Extract SkipCockroachDB to pgxtest 2022-04-02 10:35:13 -05:00
named_args.go QueryRewriter.RewriteQuery now returns an error 2022-10-29 09:33:13 -05:00
named_args_test.go QueryRewriter.RewriteQuery now returns an error 2022-10-29 09:33:13 -05:00
pgbouncer_test.go Remove ConnConfig.BuildStatementCache 2022-03-12 09:23:40 -06:00
pipeline_test.go Expose pgx functionality for manual integration with pgconn 2022-07-04 13:29:49 -05:00
query_test.go Remove unused test struct. 2023-01-16 20:06:01 -06:00
rows.go Fix: RowToStructByPos with embedded unexported struct 2023-04-27 21:03:58 -05:00
rows_test.go Fix: RowToStructByPos with embedded unexported struct 2023-04-27 21:03:58 -05:00
tracer.go Add tracing support 2022-07-16 12:27:10 -05:00
tracer_test.go Add tracing support 2022-07-16 12:27:10 -05:00
tx.go perf(tx): use strings.Builder to avoid the overhead of []byte -> string conversion 2022-11-25 12:39:22 -06:00
tx_test.go Replace Begin and BeginTx methods with functions 2022-07-09 17:25:55 -05:00
values.go CopyFrom parses strings to encode into binary format 2022-08-13 09:30:29 -05:00
values_test.go Replace interface{} with any 2022-04-09 09:12:55 -05:00

README.md

Go Reference Build Status

pgx - PostgreSQL Driver and Toolkit

pgx is a pure Go driver and toolkit for PostgreSQL.

The pgx driver is a low-level, high performance interface that exposes PostgreSQL-specific features such as LISTEN / NOTIFY and COPY. It also includes an adapter for the standard database/sql interface.

The toolkit component is a related set of packages that implement PostgreSQL functionality such as parsing the wire protocol and type mapping between PostgreSQL and Go. These underlying packages can be used to implement alternative drivers, proxies, load balancers, logical replication clients, etc.

Example Usage

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/jackc/pgx/v5"
)

func main() {
	// urlExample := "postgres://username:password@localhost:5432/database_name"
	conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
		os.Exit(1)
	}
	defer conn.Close(context.Background())

	var name string
	var weight int64
	err = conn.QueryRow(context.Background(), "select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
	if err != nil {
		fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
		os.Exit(1)
	}

	fmt.Println(name, weight)
}

See the getting started guide for more information.

Features

  • Support for approximately 70 different PostgreSQL types
  • Automatic statement preparation and caching
  • Batch queries
  • Single-round trip query mode
  • Full TLS connection control
  • Binary format support for custom types (allows for much quicker encoding/decoding)
  • COPY protocol support for faster bulk data loads
  • Tracing and logging support
  • Connection pool with after-connect hook for arbitrary connection setup
  • LISTEN / NOTIFY
  • Conversion of PostgreSQL arrays to Go slice mappings for integers, floats, and strings
  • hstore support
  • json and jsonb support
  • Maps inet and cidr PostgreSQL types to netip.Addr and netip.Prefix
  • Large object support
  • NULL mapping to pointer to pointer
  • Supports database/sql.Scanner and database/sql/driver.Valuer interfaces for custom types
  • Notice response handling
  • Simulated nested transactions with savepoints

Choosing Between the pgx and database/sql Interfaces

The pgx interface is faster. Many PostgreSQL specific features such as LISTEN / NOTIFY and COPY are not available through the database/sql interface.

The pgx interface is recommended when:

  1. The application only targets PostgreSQL.
  2. No other libraries that require database/sql are in use.

It is also possible to use the database/sql interface and convert a connection to the lower-level pgx interface as needed.

Testing

See CONTRIBUTING.md for setup instructions.

Supported Go and PostgreSQL Versions

pgx supports the same versions of Go and PostgreSQL that are supported by their respective teams. For Go that is the two most recent major releases and for PostgreSQL the major releases in the last 5 years. This means pgx supports Go 1.19 and higher and PostgreSQL 11 and higher. pgx also is tested against the latest version of CockroachDB.

Version Policy

pgx follows semantic versioning for the documented public API on stable releases. v5 is the latest stable major version.

PGX Family Libraries

github.com/jackc/pglogrepl

pglogrepl provides functionality to act as a client for PostgreSQL logical replication.

github.com/jackc/pgmock

pgmock 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).

github.com/jackc/tern

tern is a stand-alone SQL migration system.

github.com/jackc/pgerrcode

pgerrcode contains constants for the PostgreSQL error codes.

Adapters for 3rd Party Types

Adapters for 3rd Party Tracers

Adapters for 3rd Party Loggers

These adapters can be used with the tracelog package.

3rd Party Libraries with PGX Support

github.com/georgysavva/scany

Library for scanning data from a database into Go structs and more.

github.com/vingarcia/ksql

A carefully designed SQL client for making using SQL easier, more productive, and less error-prone on Golang.

https://github.com/otan/gopgkrb5

Adds GSSAPI / Kerberos authentication support.

github.com/wcamarao/pmx

Explicit data mapping and scanning library for Go structs and slices.

github.com/stephenafamo/scan

Type safe and flexible package for scanning database data into Go types. Supports, structs, maps, slices and custom mapping functions.

https://github.com/z0ne-dev/mgx

Code first migration library for native pgx (no database/sql abstraction).