PostgreSQL driver and toolkit for Go
 
 
 
Go to file
Jack Christensen 8f81acfb5f Enhance support for custom types
* Add per connection oid to name map
* Global default type format is now based on names not oids
* Add better docs for custom types
2014-09-19 15:27:15 -05:00
examples Update README.md 2014-07-17 09:17:55 -05:00
stdlib Add support for integer, float and text arrays 2014-07-26 15:03:52 -05:00
.gitignore Rename ConnectionParameters to ConnConfig 2014-05-17 13:38:13 -05:00
CHANGELOG.md Started a changelog 2014-09-16 16:59:13 -05:00
LICENSE Add license 2013-07-30 19:05:09 -05:00
README.md Enhance support for custom types 2014-09-19 15:27:15 -05:00
bench_test.go Move to Scan interface 2014-07-05 18:23:19 -05:00
conn.go Enhance support for custom types 2014-09-19 15:27:15 -05:00
conn_config_test.go.example Add TLS to conn_config_test.go.example 2014-07-29 17:41:53 -05:00
conn_pool.go discardLogger should be private 2014-09-04 16:45:35 -05:00
conn_pool_test.go Remove debug Println 2014-09-16 16:52:42 -05:00
conn_test.go Update test to work on Windows 2014-07-26 09:50:43 -05:00
doc.go Enhance support for custom types 2014-09-19 15:27:15 -05:00
example_custom_type_test.go Enhance support for custom types 2014-09-19 15:27:15 -05:00
helper_test.go Add support for integer, float and text arrays 2014-07-26 15:03:52 -05:00
logger.go Fix log truncation 2014-09-15 14:27:28 -05:00
messages.go Enhance support for custom types 2014-09-19 15:27:15 -05:00
msg_reader.go Make MsgReader private 2014-07-12 20:08:17 -05:00
query.go Fix data race with Rows and ConnPool 2014-09-16 16:29:45 -05:00
query_test.go Better error when Scan fails due to unknown destination type 2014-09-02 16:58:10 -05:00
sql.go Add QueryArgs 2014-05-21 08:17:24 -05:00
sql_test.go Update github.com/JackC to github.com/jackc 2014-06-21 08:36:20 -05:00
tx.go Tx.QueryRow implemented in terms of Tx.Query 2014-07-14 07:55:20 -05:00
tx_test.go Use database/sql style transaction interface 2014-07-12 07:59:30 -05:00
value_reader.go Added a lot of documentation 2014-07-12 21:17:38 -05:00
values.go Enhance support for custom types 2014-09-19 15:27:15 -05:00
values_test.go Always use bound parameters 2014-07-18 16:51:11 -05:00

README.md

pgx

PostgreSQL client library for Go

Description

pgx is a database connection library designed specifically for PostgreSQL. pgx offers an interface similar to database/sql that offers more performance and features than are available the database/sql interface. It also can run as a database/sql compatible driver by importing github.com/jackc/pgx/stdlib.

Features

Below are some of the standout features of pgx.

Familiar Query Interface

pgx implements Query, QueryRow, and Scan in the familiar database/sql style.

var name string
var weight int64
err := conn.QueryRow("select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
if err != nil {
    return err
}

pgx adds convenience to Query in that it is only necessary to call Close if you want to ignore the rest of the rows. When Next has read all rows or an error occurs, the rows are closed automatically.

var sum int32

rows, err := conn.Query("select generate_series(1,$1)", 10)
if err != nil {
    t.Fatalf("conn.Query failed: ", err)
}

for rows.Next() {
    var n int32
    rows.Scan(&n)
    sum += n
}

// rows.Close implicitly called when rows.Next is finished

if rows.Err() != nil {
    t.Fatalf("conn.Query failed: ", err)
}

// ...

Prepared Statements

Prepared statements are easy to use in pgx. Just call Prepare with the name of the statement and the SQL. To execute a prepared statement just pass the name of the statement into a Query, QueryRow, or Exec as the SQL text. It will automatically detect that it is the name of a prepared statement and execute it.

if _, err := conn.Prepare("getTime", "select now()"); err == nil {
    // handle err
}

var t time.Time
err := conn.QueryRow("getTime").Scan(&t)
if err != nil {
    return err
}

Explicit Connection Pool

Connection pool usage is explicit and configurable. In pgx, a connection can be created and managed directly, or a connection pool with a configurable maximum connections can be used. Also, the connection pool offers an after connect hook that allows every connection to be automatically setup before being made available in the connection pool. This is especially useful to ensure all connections have the same prepared statements available or to change any other connection settings.

It delegates Query, QueryRow, Exec, and Begin functions to an automatically checked out and released connection so you can avoid manually acquiring and releasing connections when you do not need that level of control.

var name string
var weight int64
err := pool.QueryRow("select name, weight from widgets where id=$1", 42).Scan(&name, &weight)
if err != nil {
    return err
}

Transactions

Transactions are started by calling Begin or BeginIso. The BeginIso variant creates a transaction with a specified isolation level.

    tx, err := conn.Begin()
    if err != nil {
        t.Fatalf("conn.Begin failed: %v", err)
    }

    _, err = tx.Exec("insert into foo(id) values (1)")
    if err != nil {
        t.Fatalf("tx.Exec failed: %v", err)
    }

    err = tx.Commit()
    if err != nil {
        t.Fatalf("tx.Commit failed: %v", err)
    }
})

Listen / Notify

Pgx can listen to the PostgreSQL notification system with the WaitForNotification function. It takes a maximum time to wait for a notification.

if notification, err := conn.WaitForNotification(time.Second); err != nil {
    // do something with notification
}

TLS

The pgx ConnConfig struct has a TLSConfig field. If this field is nil, then TLS will be disabled. If it is present, then it will be used to configure the TLS connection.

Custom Type Support

pgx includes support for the common data types like integers, floats, strings, dates, and times that have direct mappings between Go and SQL. Support can be added for additional types like point, hstore, numeric, etc. that do not have direct mappings in Go. See the documentation for more information.

Null Mapping

pgx includes Null* types in a similar fashion to database/sql that implement the necessary interfaces to be encoded and scanned.

Array Mapping

pgx maps between int16, int32, int64, float32, float64, and string Go slices and the equivalent PostgreSQL array type. Go slices of native types do not support nulls, so if a PostgreSQL array that contains a slice is read into a native Go slice an error will occur.

Logging

pgx defines a simple logger interface. Connections optionally accept a logger that satisfies this interface. The log15 package satisfies this interface and it is simple to define adapters for other loggers.

Testing

Pgx supports multiple connection and authentication types. Setting up a test environment that can test all of them can be cumbersome. In particular, Windows cannot test Unix domain socket connections. Because of this pgx will skip tests for connection types that are not configured.

Normal Test Environment

To setup the normal test environment run the following SQL:

create user pgx_md5 password 'secret';
create database pgx_test;

Next open connection_settings_test.go.example and make a copy without the .example. If your PostgreSQL server is accepting connections on 127.0.0.1, then you are done.

Connection and Authentication Test Environment

Complete the normal test environment setup and also do the following.

Run the following SQL:

create user pgx_none;
create user pgx_pw password 'secret';

Add the following to your pg_hba.conf:

If you are developing on Unix with domain socket connections:

local  pgx_test  pgx_none  trust
local  pgx_test  pgx_pw    password
local  pgx_test  pgx_md5   md5

If you are developing on Windows with TCP connections:

host  pgx_test  pgx_none  127.0.0.1/32 trust
host  pgx_test  pgx_pw    127.0.0.1/32 password
host  pgx_test  pgx_md5   127.0.0.1/32 md5