Documentation ¶
Overview ¶
Package pq is a pure Go Postgres driver for the database/sql package.
In most cases clients will use the database/sql package instead of using this package directly. For example:
import ( _ "github.com/adjust/pq" "database/sql" ) func main() { db, err := sql.Open("postgres", "user=pqgotest dbname=pqgotest sslmode=verify-full") if err != nil { log.Fatal(err) } age := 21 rows, err := db.Query("SELECT name FROM users WHERE age=$1", age) … }
You can also connect to a database using a URL. For example:
db, err := sql.Open("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full")
Note that database/sql does not dictate any specific format for parameter markers in query strings, and pq uses the Postgres-native ordinal markers, as shown above. The same marker can be reused for the same parameter:
rows, err := db.Query(`SELECT name FROM users WHERE favorite_fruit = $1 OR age BETWEEN $2 AND $2 + 3`, "orange", 64)
Note also that pq does not support the LastInsertId() method of the Result type in database/sql. To return the identifier of an INSERT (or UPDATE or DELETE), use the Postgres RETURNING clause with a standard Query or QueryRow call:
rows, err := db.Query(`INSERT INTO users(name, favorite_fruit, age) VALUES('beatrice', 'starfruit', 93) RETURNING id`)
For more details on RETURNING, see the Postgres documentation:
http://www.postgresql.org/docs/current/static/sql-insert.html http://www.postgresql.org/docs/current/static/sql-update.html http://www.postgresql.org/docs/current/static/sql-delete.html
Index ¶
Constants ¶
const ( Efatal = "FATAL" Epanic = "PANIC" Ewarning = "WARNING" Enotice = "NOTICE" Edebug = "DEBUG" Einfo = "INFO" Elog = "LOG" )
Error severities
Variables ¶
var ( ErrSSLNotSupported = errors.New("pq: SSL is not enabled on the server") ErrNotSupported = errors.New("pq: Unsupported command") )
Common error types
Functions ¶
func ParseURL ¶
ParseURL no longer needs to be used by clients of this library since supplying a URL as a connection string to sql.Open() is now supported:
sql.Open("postgres", "postgres://bob:secret@1.2.3.4:5432/mydb?sslmode=verify-full")
It remains exported here for backwards-compatibility.
ParseURL converts a url to a connection string for driver.Open. Example:
"postgres://bob:secret@1.2.3.4:5432/mydb?sslmode=verify-full"
converts to:
"user=bob password=secret host=1.2.3.4 port=5432 dbname=mydb sslmode=verify-full"
A minimal example:
"postgres://"
This will be blank, causing driver.Open to use all of the defaults
Types ¶
type Error ¶
type Error struct { Severity string Code ErrorCode Message string Detail string Hint string Position string InternalPosition string InternalQuery string Where string Schema string Table string Column string DataTypeName string Constraint string File string Line string Routine string }
Error represents an error communicating with the server.
See http://www.postgresql.org/docs/current/static/protocol-error-fields.html for details of the fields
type ErrorCode ¶
type ErrorCode string
ErrorCode is a five digit pq error code
func (ErrorCode) Name ¶
Name returns a more human friendly rendering of the error code
See http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html for details