|
| 1 | +# pgx |
| 2 | + |
| 3 | +PostgreSQL client library for Go |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +Below are some of the standout features of pgx. |
| 12 | + |
| 13 | +### Familiar Query Interface |
| 14 | + |
| 15 | +pgx implements Query, QueryRow, and Scan in the familiar database/sql style. |
| 16 | + |
| 17 | +```go |
| 18 | +var name string |
| 19 | +var weight int64 |
| 20 | +err := conn.QueryRow("select name, weight from widgets where id=$1", 42).Scan(&name, &weight) |
| 21 | +if err != nil { |
| 22 | + return err |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +pgx adds convenience to Query in that it is only necessary to call Close if you |
| 27 | +want to ignore the rest of the rows. When Next has read all rows or an error |
| 28 | +occurs, the rows are closed automatically. |
| 29 | + |
| 30 | +```go |
| 31 | +var sum int32 |
| 32 | + |
| 33 | +rows, err := conn.Query("select generate_series(1,$1)", 10) |
| 34 | +if err != nil { |
| 35 | + t.Fatalf("conn.Query failed: ", err) |
| 36 | +} |
| 37 | + |
| 38 | +for rows.Next() { |
| 39 | + var n int32 |
| 40 | + rows.Scan(&n) |
| 41 | + sum += n |
| 42 | +} |
| 43 | + |
| 44 | +// rows.Close implicitly called when rows.Next is finished |
| 45 | + |
| 46 | +if rows.Err() != nil { |
| 47 | + t.Fatalf("conn.Query failed: ", err) |
| 48 | +} |
| 49 | + |
| 50 | +// ... |
| 51 | +``` |
| 52 | + |
| 53 | +### Prepared Statements |
| 54 | + |
| 55 | +Prepared statements are easy to use in pgx. Just call Prepare with the name of |
| 56 | +the statement and the SQL. To execute a prepared statement just pass the name |
| 57 | +of the statement into a Query, QueryRow, or Exec as the SQL text. It will |
| 58 | +automatically detect that it is the name of a prepared statement and execute |
| 59 | +it. |
| 60 | + |
| 61 | +```go |
| 62 | +if _, err := conn.Prepare("getTime", "select now()"); err == nil { |
| 63 | + // handle err |
| 64 | +} |
| 65 | + |
| 66 | +var t time.Time |
| 67 | +err := conn.QueryRow("getTime").Scan(&t) |
| 68 | +if err != nil { |
| 69 | + return err |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +Prepared statements will use the binary transmission when possible. This can |
| 74 | +substantially increase performance. |
| 75 | + |
| 76 | +### Explicit Connection Pool |
| 77 | + |
| 78 | +Connection pool usage is explicit and configurable. In pgx, a connection can |
| 79 | +be created and managed directly, or a connection pool with a configurable |
| 80 | +maximum connections can be used. Also, the connection pool offers an after |
| 81 | +connect hook that allows every connection to be automatically setup before |
| 82 | +being made available in the connection pool. This is especially useful to |
| 83 | +ensure all connections have the same prepared statements available or to |
| 84 | +change any other connection settings. |
| 85 | + |
| 86 | +It delegates Query, QueryRow, Exec, and Begin functions to an automatically |
| 87 | +checked out and released connection so you can avoid manually acquiring and |
| 88 | +releasing connections when you do not need that level of control. |
| 89 | + |
| 90 | +```go |
| 91 | +var name string |
| 92 | +var weight int64 |
| 93 | +err := pool.QueryRow("select name, weight from widgets where id=$1", 42).Scan(&name, &weight) |
| 94 | +if err != nil { |
| 95 | + return err |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### Transactions |
| 100 | + |
| 101 | +Transactions are started by calling Begin or BeginIso. The BeginIso variant |
| 102 | +creates a transaction with a specified isolation level. |
| 103 | + |
| 104 | +```go |
| 105 | + tx, err := conn.Begin() |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("conn.Begin failed: %v", err) |
| 108 | + } |
| 109 | + |
| 110 | + _, err = tx.Exec("insert into foo(id) values (1)") |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("tx.Exec failed: %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + err = tx.Commit() |
| 116 | + if err != nil { |
| 117 | + t.Fatalf("tx.Commit failed: %v", err) |
| 118 | + } |
| 119 | +}) |
| 120 | +``` |
| 121 | + |
| 122 | +### Listen / Notify |
| 123 | + |
| 124 | +Pgx can listen to the PostgreSQL notification system with the |
| 125 | +WaitForNotification function. It takes a maximum time to wait for a |
| 126 | +notification. |
| 127 | + |
| 128 | +```go |
| 129 | +if notification, err := conn.WaitForNotification(time.Second); err != nil { |
| 130 | + // do something with notification |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +### TLS |
| 135 | + |
| 136 | +The pgx ConnConfig struct has a TLSConfig field. If this field is |
| 137 | +nil, then TLS will be disabled. If it is present, then it will be used to |
| 138 | +configure the TLS connection. |
| 139 | + |
| 140 | +### Custom Type Support |
| 141 | + |
| 142 | +pgx includes support for the common data types like integers, floats, strings, |
| 143 | +dates, and times that have direct mappings between Go and SQL. Support can be |
| 144 | +added for additional types like point, hstore, numeric, etc. that do not have |
| 145 | +direct mappings in Go by the types implementing Scanner, TextEncoder, and |
| 146 | +optionally BinaryEncoder. To enable binary format for custom types, a prepared |
| 147 | +statement must be used and the field description of the returned field must have |
| 148 | +FormatCode set to BinaryFormatCode. See example_custom_type_test.go for an |
| 149 | +example of a custom type for the PostgreSQL point type. |
| 150 | + |
| 151 | +### Null Mapping |
| 152 | + |
| 153 | +pgx includes Null* types in a similar fashion to database/sql that implement the |
| 154 | +necessary interfaces to be encoded and scanned. |
| 155 | + |
| 156 | +### Logging |
| 157 | + |
| 158 | +pgx connections optionally accept a logger from the [log15 package](http://gopkg.in/inconshreveable/log15.v2). |
| 159 | + |
| 160 | +## Testing |
| 161 | + |
| 162 | +Pgx supports multiple connection and authentication types. Setting up a test |
| 163 | +environment that can test all of them can be cumbersome. In particular, |
| 164 | +Windows cannot test Unix domain socket connections. Because of this pgx will |
| 165 | +skip tests for connection types that are not configured. |
| 166 | + |
| 167 | +### Normal Test Environment |
| 168 | + |
| 169 | +To setup the normal test environment run the following SQL: |
| 170 | + |
| 171 | + create user pgx_md5 password 'secret'; |
| 172 | + create database pgx_test; |
| 173 | + |
| 174 | +Next open connection_settings_test.go.example and make a copy without the |
| 175 | +.example. If your PostgreSQL server is accepting connections on 127.0.0.1, |
| 176 | +then you are done. |
| 177 | + |
| 178 | +### Connection and Authentication Test Environment |
| 179 | + |
| 180 | +Complete the normal test environment setup and also do the following. |
| 181 | + |
| 182 | +Run the following SQL: |
| 183 | + |
| 184 | + create user pgx_none; |
| 185 | + create user pgx_pw password 'secret'; |
| 186 | + |
| 187 | +Add the following to your pg_hba.conf: |
| 188 | + |
| 189 | +If you are developing on Unix with domain socket connections: |
| 190 | + |
| 191 | + local pgx_test pgx_none trust |
| 192 | + local pgx_test pgx_pw password |
| 193 | + local pgx_test pgx_md5 md5 |
| 194 | + |
| 195 | +If you are developing on Windows with TCP connections: |
| 196 | + |
| 197 | + host pgx_test pgx_none 127.0.0.1/32 trust |
| 198 | + host pgx_test pgx_pw 127.0.0.1/32 password |
| 199 | + host pgx_test pgx_md5 127.0.0.1/32 md5 |
0 commit comments