添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
  • Handles bad connections for database/sql
  • Scan time.Time correctly (i.e. timestamp[tz] , time[tz] , date )
  • Scan binary blobs correctly (i.e. bytea )
  • Package for hstore support
  • COPY FROM support
  • pq.ParseURL for converting urls to connection strings for sql.Open.
  • Many libpq compatible environment variables
  • Unix socket support
  • Notifications: LISTEN / NOTIFY
  • pgpass support
  • GSS (Kerberos) auth
  • Tests

    go test is used for testing. See TESTS.md for more details.

    Status

    This package is currently in maintenance mode, which means:

  • It generally does not accept new features.
  • It does accept bug fixes and version compatability changes provided by the community.
  • Maintainers usually do not resolve reported issues.
  • Community members are encouraged to help each other with reported issues.
  • For users that require new features or reliable resolution of reported bugs, we recommend using pgx which is under active development.

    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 (
    	"database/sql"
    	_ "github.com/lib/pq"
    func main() {
    	connStr := "user=pqgotest dbname=pqgotest sslmode=verify-full"
    	db, err := sql.Open("postgres", connStr)
    	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:

    connStr := "postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full"
    db, err := sql.Open("postgres", connStr)
    

    Connection String Parameters

    Similarly to libpq, when establishing a connection using pq you are expected to supply a connection string containing zero or more parameters. A subset of the connection parameters supported by libpq are also supported by pq. Additionally, pq also lets you specify run-time parameters (such as search_path or work_mem) directly in the connection string. This is different from libpq, which does not allow run-time parameters in the connection string, instead requiring you to supply them in the options parameter.

    For compatibility with libpq, the following special connection parameters are supported:

  • dbname - The name of the database to connect to
  • user - The user to sign in as
  • password - The user's password
  • host - The host to connect to. Values that start with / are for unix domain sockets. (default is localhost)
  • port - The port to bind to. (default is 5432)
  • sslmode - Whether or not to use SSL (default is require, this is not the default for libpq)
  • fallback_application_name - An application_name to fall back to if one isn't provided.
  • connect_timeout - Maximum wait for connection, in seconds. Zero or not specified means wait indefinitely.
  • sslcert - Cert file location. The file must contain PEM encoded data.
  • sslkey - Key file location. The file must contain PEM encoded data.
  • sslrootcert - The location of the root certificate file. The file must contain PEM encoded data.
  • Valid values for sslmode are:

  • disable - No SSL
  • require - Always SSL (skip verification)
  • verify-ca - Always SSL (verify that the certificate presented by the server was signed by a trusted CA)
  • verify-full - Always SSL (verify that the certification presented by the server was signed by a trusted CA and the server host name matches the one in the certificate)
  • See http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING for more information about connection string parameters.

    Use single quotes for values that contain whitespace:

    "user=pqgotest password='with spaces'"
    

    A backslash will escape the next character in values:

    "user=space\ man password='it\'s valid'"
    

    Note that the connection parameter client_encoding (which sets the text encoding for the connection) may be set but must be "UTF8", matching with the same rules as Postgres. It is an error to provide any other value.

    In addition to the parameters listed above, any run-time parameter that can be set at backend start time can be set in the connection string. For more information, see http://www.postgresql.org/docs/current/static/runtime-config.html .

    Most environment variables as specified at http://www.postgresql.org/docs/current/static/libpq-envars.html supported by libpq are also supported by pq. If any of the environment variables not supported by pq are set, pq will panic during connection establishment. Environment variables have a lower precedence than explicitly provided connection parameters.

    The pgpass mechanism as described in http://www.postgresql.org/docs/current/static/libpq-pgpass.html is supported, but on Windows PGPASSFILE must be specified explicitly.

    Queries

    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)
    

    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:

    var userid int
    err := db.QueryRow(`INSERT INTO users(name, favorite_fruit, age)
    	VALUES('beatrice', 'starfruit', 93) RETURNING id`).Scan(&userid)
    

    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
    

    For additional instructions on querying see the documentation for the database/sql package.

    Data Types

    Parameters pass through driver.DefaultParameterConverter before they are handled by this package. When the binary_parameters connection option is enabled, []byte values are sent directly to the backend as data in binary format.

    This package returns the following types for values from the PostgreSQL backend:

  • integer types smallint, integer, and bigint are returned as int64
  • floating-point types real and double precision are returned as float64
  • character types char, varchar, and text are returned as string
  • temporal types date, time, timetz, timestamp, and timestamptz are returned as time.Time
  • the boolean type is returned as bool
  • the bytea type is returned as []byte
  • All other types are returned directly from the backend as []byte values in text format.

    Errors

    pq may return errors of type *pq.Error which can be interrogated for error details:

    if err, ok := err.(*pq.Error); ok {
        fmt.Println("pq error:", err.Code.Name())
    

    See the pq.Error type for details.

    Bulk imports

    You can perform bulk imports by preparing a statement returned by pq.CopyIn (or pq.CopyInSchema) in an explicit transaction (sql.Tx). The returned statement handle can then be repeatedly "executed" to copy data into the target table. After all data has been processed you should call Exec() once with no arguments to flush all buffered data. Any call to Exec() might return an error which should be handled appropriately, but because of the internal buffering an error returned by Exec() might not be related to the data passed in the call that failed.

    CopyIn uses COPY FROM internally. It is not possible to COPY outside of an explicit transaction in pq.

    Usage example:

    txn, err := db.Begin()
    if err != nil {
    	log.Fatal(err)
    stmt, err := txn.Prepare(pq.CopyIn("users", "name", "age"))
    if err != nil {
    	log.Fatal(err)
    for _, user := range users {
    	_, err = stmt.Exec(user.Name, int64(user.Age))
    	if err != nil {
    		log.Fatal(err)
    _, err = stmt.Exec()
    if err != nil {
    	log.Fatal(err)
    err = stmt.Close()
    if err != nil {
    	log.Fatal(err)
    err = txn.Commit()
    if err != nil {
    	log.Fatal(err)
    

    Notifications

    PostgreSQL supports a simple publish/subscribe model over database connections. See http://www.postgresql.org/docs/current/static/sql-notify.html for more information about the general mechanism.

    To start listening for notifications, you first have to open a new connection to the database by calling NewListener. This connection can not be used for anything other than LISTEN / NOTIFY. Calling Listen will open a "notification channel"; once a notification channel is open, a notification generated on that channel will effect a send on the Listener.Notify channel. A notification channel will remain open until Unlisten is called, though connection loss might result in some notifications being lost. To solve this problem, Listener sends a nil pointer over the Notify channel any time the connection is re-established following a connection loss. The application can get information about the state of the underlying connection by setting an event callback in the call to NewListener.

    A single Listener can safely be used from concurrent goroutines, which means that there is often no need to create more than one Listener in your application. However, a Listener is always connected to a single database, so you will need to create a new Listener instance for every database you want to receive notifications in.

    The channel name in both Listen and Unlisten is case sensitive, and can contain any characters legal in an identifier (see http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS for more information). Note that the channel name will be truncated to 63 bytes by the PostgreSQL server.

    You can find a complete, working example of Listener usage at https://godoc.org/github.com/lib/pq/example/listen .

    Kerberos Support

    If you need support for Kerberos authentication, add the following to your main package:

    import "github.com/lib/pq/auth/kerberos"
    func init() {
    	pq.RegisterGSSProvider(func() (pq.Gss, error) { return kerberos.NewGSS() })
    

    This package is in a separate module so that users who don't need Kerberos don't have to download unnecessary dependencies.

    When imported, additional connection string parameters are supported:

  • krbsrvname - GSS (Kerberos) service name when constructing the SPN (default is `postgres`). This will be combined with the host to form the full SPN: `krbsrvname/host`.
  • krbspn - GSS (Kerberos) SPN. This takes priority over `krbsrvname` if present.
  • Index

  • Constants
  • Variables
  • func Array(a interface{}) interface{ ... } func BufferQuoteIdentifier(name string, buffer *bytes.Buffer) func ConnectorNoticeHandler(c driver.Connector) func(*Error) func ConnectorNotificationHandler(c driver.Connector) func(*Notification) func CopyIn(table string, columns ...string) string func CopyInSchema(schema, table string, columns ...string) string func DialOpen(d Dialer, dsn string) (_ driver.Conn, err error) func EnableInfinityTs(negative time.Time, positive time.Time) func FormatTimestamp(t time.Time) []byte func NoticeHandler(c driver.Conn) func(*Error) func Open(dsn string) (_ driver.Conn, err error) func ParseTimestamp(currentLocation *time.Location, str string) (time.Time, error) func ParseURL(url string) (string, error) func QuoteIdentifier(name string) string func QuoteLiteral(literal string) string func RegisterGSSProvider(newGssArg NewGSSFunc) func SetNoticeHandler(c driver.Conn, handler func(*Error)) func SetNotificationHandler(c driver.Conn, handler func(*Notification)) type ArrayDelimiter type BoolArray func (a *BoolArray) Scan(src interface{}) error func (a BoolArray) Value() (driver.Value, error) type ByteaArray func (a *ByteaArray) Scan(src interface{}) error func (a ByteaArray) Value() (driver.Value, error) type Connector func NewConnector(dsn string) (*Connector, error) func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) func (c *Connector) Dialer(dialer Dialer) func (c *Connector) Driver() driver.Driver type Dialer type DialerContext type Driver func (d Driver) Open(name string) (driver.Conn, error) type Error func (err *Error) Error() string func (err *Error) Fatal() bool func (err *Error) Get(k byte) (v string) func (err *Error) SQLState() string type ErrorClass func (ec ErrorClass) Name() string type ErrorCode func (ec ErrorCode) Class() ErrorClass func (ec ErrorCode) Name() string type EventCallbackType type Float32Array func (a *Float32Array) Scan(src interface{}) error func (a Float32Array) Value() (driver.Value, error) type Float64Array func (a *Float64Array) Scan(src interface{}) error func (a Float64Array) Value() (driver.Value, error) type GSS type GenericArray func (a GenericArray) Scan(src interface{}) error func (a GenericArray) Value() (driver.Value, error) type Int32Array func (a *Int32Array) Scan(src interface{}) error func (a Int32Array) Value() (driver.Value, error) type Int64Array func (a *Int64Array) Scan(src interface{}) error func (a Int64Array) Value() (driver.Value, error) type Listener func NewDialListener(d Dialer, name string, minReconnectInterval time.Duration, ...) *Listener func NewListener(name string, minReconnectInterval time.Duration, ...) *Listener func (l *Listener) Close() error func (l *Listener) Listen(channel string) error func (l *Listener) NotificationChannel() <-chan *Notification func (l *Listener) Ping() error func (l *Listener) Unlisten(channel string) error func (l *Listener) UnlistenAll() error type ListenerConn func NewListenerConn(name string, notificationChan chan<- *Notification) (*ListenerConn, error) func (l *ListenerConn) Close() error func (l *ListenerConn) Err() error func (l *ListenerConn) ExecSimpleQuery(q string) (executed bool, err error) func (l *ListenerConn) Listen(channel string) (bool, error) func (l *ListenerConn) Ping() error func (l *ListenerConn) Unlisten(channel string) (bool, error) func (l *ListenerConn) UnlistenAll() (bool, error) type ListenerEventType type NewGSSFunc type NoticeHandlerConnector func ConnectorWithNoticeHandler(c driver.Connector, handler func(*Error)) *NoticeHandlerConnector func (n *NoticeHandlerConnector) Connect(ctx context.Context) (driver.Conn, error) type Notification type NotificationHandlerConnector func ConnectorWithNotificationHandler(c driver.Connector, handler func(*Notification)) *NotificationHandlerConnector func (n *NotificationHandlerConnector) Connect(ctx context.Context) (driver.Conn, error) type NullTime func (nt *NullTime) Scan(value interface{}) error func (nt NullTime) Value() (driver.Value, error) type PGError type StringArray func (a *StringArray) Scan(src interface{}) error func (a StringArray) Value() (driver.Value, error)

    Examples

  • ConnectorWithNoticeHandler
  • NewConnector
  • Constants

    View Source
    const (
    	Efatal   = "FATAL"
    	Epanic   = "PANIC"
    	Ewarning = "WARNING"
    	Enotice  = "NOTICE"
    	Edebug   = "DEBUG"
    	Einfo    = "INFO"
    	Elog     = "LOG"
      

    Error severities

    Variables

    View Source
    var (
    	ErrNotSupported              = errors.New("pq: Unsupported command")
    	ErrInFailedTransaction       = errors.New("pq: Could not complete operation in a failed transaction")
    	ErrSSLNotSupported           = errors.New("pq: SSL is not enabled on the server")
    	ErrSSLKeyUnknownOwnership    = errors.New("pq: Could not get owner information for private key, may not be properly protected")
    	ErrSSLKeyHasWorldPermissions = errors.New("pq: Private key has world access. Permissions should be u=rw,g=r (0640) if owned by root, or u=rw (0600), or less")
    	ErrCouldNotDetectUsername = errors.New("pq: Could not detect default username. Please provide one explicitly")
      

    Common error types View Source

    var ErrChannelAlreadyOpen = errors.New("pq: channel is already open")

    ErrChannelAlreadyOpen is returned from Listen when a channel is already open. View Source

    var ErrChannelNotOpen = errors.New("pq: channel is not open")

    ErrChannelNotOpen is returned from Unlisten when a channel is not open.

    Functions

    Array returns the optimal driver.Valuer and sql.Scanner for an array or slice of any dimension.

    For example:

    db.Query(`SELECT * FROM t WHERE id = ANY($1)`, pq.Array([]int{235, 401}))
    var x []sql.NullInt64
    db.QueryRow(`SELECT ARRAY[235, 401]`).Scan(pq.Array(&x))
    

    Scanning multi-dimensional arrays is not supported. Arrays where the lower bound is not one (such as `[0:0]={1}') are not supported.

    func BufferQuoteIdentifier(name string, buffer *bytes.Buffer)

    BufferQuoteIdentifier satisfies the same purpose as QuoteIdentifier, but backed by a byte buffer.

    func ConnectorNoticeHandler(c driver.Connector) func(*Error)

    ConnectorNoticeHandler returns the currently set notice handler, if any. If the given connector is not a result of ConnectorWithNoticeHandler, nil is returned.

    func ConnectorNotificationHandler(c driver.Connector) func(*Notification)

    ConnectorNotificationHandler returns the currently set notification handler, if any. If the given connector is not a result of ConnectorWithNotificationHandler, nil is returned.

    func CopyIn(table string, columns ...string) string

    CopyIn creates a COPY FROM statement which can be prepared with Tx.Prepare(). The target table should be visible in search_path.

    func CopyInSchema(schema, table string, columns ...string) string

    CopyInSchema creates a COPY FROM statement which can be prepared with Tx.Prepare().

    func EnableInfinityTs(negative time.Time, positive time.Time)

    EnableInfinityTs controls the handling of Postgres' "-infinity" and "infinity" "timestamp"s.

    If EnableInfinityTs is not called, "-infinity" and "infinity" will return []byte("-infinity") and []byte("infinity") respectively, and potentially cause error "sql: Scan error on column index 0: unsupported driver -> Scan pair: []uint8 -> *time.Time", when scanning into a time.Time value.

    Once EnableInfinityTs has been called, all connections created using this driver will decode Postgres' "-infinity" and "infinity" for "timestamp", "timestamp with time zone" and "date" types to the predefined minimum and maximum times, respectively. When encoding time.Time values, any time which equals or precedes the predefined minimum time will be encoded to "-infinity". Any values at or past the maximum time will similarly be encoded to "infinity".

    If EnableInfinityTs is called with negative >= positive, it will panic. Calling EnableInfinityTs after a connection has been established results in undefined behavior. If EnableInfinityTs is called more than once, it will panic.

    func NoticeHandler(c driver.Conn) func(*Error)

    NoticeHandler returns the notice handler on the given connection, if any. A runtime panic occurs if c is not a pq connection. This is rarely used directly, use ConnectorNoticeHandler and ConnectorWithNoticeHandler instead.

    func Open(dsn string) (_ driver.Conn, err error)

    Open opens a new connection to the database. dsn is a connection string. Most users should only use it through database/sql package from the standard library.

    func ParseTimestamp(currentLocation *time.Location, str string) (time.Time, error)

    ParseTimestamp parses Postgres' text format. It returns a time.Time in currentLocation iff that time's offset agrees with the offset sent from the Postgres server. Otherwise, ParseTimestamp returns a time.Time with the fixed offset offset provided by the Postgres server.

    func ParseURL(url string) (string, error)

    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

    func QuoteIdentifier(name string) string

    QuoteIdentifier quotes an "identifier" (e.g. a table or a column name) to be used as part of an SQL statement. For example:

    tblname := "my_table"
    data := "my_data"
    quoted := pq.QuoteIdentifier(tblname)
    err := db.Exec(fmt.Sprintf("INSERT INTO %s VALUES ($1)", quoted), data)
    

    Any double quotes in name will be escaped. The quoted identifier will be case sensitive when used in a query. If the input string contains a zero byte, the result will be truncated immediately before it.

    func QuoteLiteral(literal string) string

    QuoteLiteral quotes a 'literal' (e.g. a parameter, often used to pass literal to DDL and other statements that do not accept parameters) to be used as part of an SQL statement. For example:

    exp_date := pq.QuoteLiteral("2023-01-05 15:00:00Z")
    err := db.Exec(fmt.Sprintf("CREATE ROLE my_user VALID UNTIL %s", exp_date))
    

    Any single quotes in name will be escaped. Any backslashes (i.e. "\") will be replaced by two backslashes (i.e. "\\") and the C-style escape identifier that PostgreSQL provides ('E') will be prepended to the string.

    func RegisterGSSProvider(newGssArg NewGSSFunc)

    RegisterGSSProvider registers a GSS authentication provider. For example, if you need to use Kerberos to authenticate with your server, add this to your main package:

    import "github.com/lib/pq/auth/kerberos"
    func init() {
    	pq.RegisterGSSProvider(func() (pq.GSS, error) { return kerberos.NewGSS() })
          
    func SetNoticeHandler(c driver.Conn, handler func(*Error))

    SetNoticeHandler sets the given notice handler on the given connection. A runtime panic occurs if c is not a pq connection. A nil handler may be used to unset it. This is rarely used directly, use ConnectorNoticeHandler and ConnectorWithNoticeHandler instead.

    Note: Notice handlers are executed synchronously by pq meaning commands won't continue to be processed until the handler returns.

    func SetNotificationHandler(c driver.Conn, handler func(*Notification))

    SetNotificationHandler sets the given notification handler on the given connection. A runtime panic occurs if c is not a pq connection. A nil handler may be used to unset it.

    Note: Notification handlers are executed synchronously by pq meaning commands won't continue to be processed until the handler returns.

    type ArrayDelimiter interface {
    	// ArrayDelimiter returns the delimiter character(s) for this element's type.
    	ArrayDelimiter() string
      

    ArrayDelimiter may be optionally implemented by driver.Valuer or sql.Scanner to override the array delimiter used by GenericArray.

    func (a ByteaArray) Value() (driver.Value, error)

    Value implements the driver.Valuer interface. It uses the "hex" format which is only supported on PostgreSQL 9.0 or newer.

    Connector represents a fixed configuration for the pq driver with a given name. Connector satisfies the database/sql/driver Connector interface and can be used to create any number of DB Conn's via the database/sql OpenDB function.

    See https://golang.org/pkg/database/sql/driver/#Connector. See https://golang.org/pkg/database/sql/#OpenDB.

    func NewConnector(dsn string) (*Connector, error)

    NewConnector returns a connector for the pq driver in a fixed configuration with the given dsn. The returned connector can be used to create any number of equivalent Conn's. The returned connector is intended to be used with database/sql.OpenDB.

    See https://golang.org/pkg/database/sql/driver/#Connector. See https://golang.org/pkg/database/sql/#OpenDB.

    Example package main import ( "database/sql" "fmt" "github.com/lib/pq" func main() { name := "" connector, err := pq.NewConnector(name) if err != nil { fmt.Println(err) return db := sql.OpenDB(connector) defer db.Close() // Use the DB txn, err := db.Begin() if err != nil { fmt.Println(err) return txn.Rollback()
    Output:
          
    func (c *Connector) Connect(ctx context.Context) (driver.Conn, error)

    Connect returns a connection to the database using the fixed configuration of this Connector. Context is not used.

    type Dialer interface {
    	Dial(network, address string) (net.Conn, error)
    	DialTimeout(network, address string, timeout time.Duration) (net.Conn, error)
      

    Dialer is the dialer interface. It can be used to obtain more control over how pq creates network connections.

    type DialerContext interface {
    	DialContext(ctx context.Context, network, address string) (net.Conn, error)
      

    DialerContext is the context-aware dialer interface.

    func (d Driver) Open(name string) (driver.Conn, error)

    Open opens a new connection to the database. name is a connection string. Most users should only use it through database/sql package from the standard library.

    func (err *Error) Get(k byte) (v string)

    Get implements the legacy PGError interface. New code should use the fields of the Error struct directly.

    func (ec ErrorClass) Name() string

    Name returns the condition name of an error class. It is equivalent to the condition name of the "standard" error code (i.e. the one having the last three characters "000").

    Class returns the error class, e.g. "28".

    See http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html for details.

    func (ec ErrorCode) Name() string

    Name returns a more human friendly rendering of the error code, namely the "condition name".

    See http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html for details.

    type EventCallbackType func(event ListenerEventType, err error)

    EventCallbackType is the event callback type. See also ListenerEventType constants' documentation.

    type Float32Array []float32

    Float32Array represents a one-dimensional array of the PostgreSQL double precision type.

    type Float64Array []float64

    Float64Array represents a one-dimensional array of the PostgreSQL double precision type.

    type GSS interface {
    	GetInitToken(host string, service string) ([]byte, error)
    	GetInitTokenFromSpn(spn string) ([]byte, error)
    	Continue(inToken []byte) (done bool, outToken []byte, err error)
      

    GSS provides GSSAPI authentication (e.g., Kerberos).

    type GenericArray struct{ A interface{} }

    GenericArray implements the driver.Valuer and sql.Scanner interfaces for an array or slice of any dimension.

    type Listener struct {
    	// Channel for receiving notifications from the database.  In some cases a
    	// nil value will be sent.  See section "Notifications" above.
    	Notify chan *Notification
    	// contains filtered or unexported fields
      

    Listener provides an interface for listening to notifications from a PostgreSQL database. For general usage information, see section "Notifications".

    Listener can safely be used from concurrently running goroutines. name string, minReconnectInterval time.Duration, maxReconnectInterval time.Duration, eventCallback EventCallbackType) *Listener

    NewDialListener is like NewListener but it takes a Dialer.

    func NewListener(name string,
    	minReconnectInterval time.Duration,
    	maxReconnectInterval time.Duration,
    	eventCallback EventCallbackType) *Listener

    NewListener creates a new database connection dedicated to LISTEN / NOTIFY.

    name should be set to a connection string to be used to establish the database connection (see section "Connection String Parameters" above).

    minReconnectInterval controls the duration to wait before trying to re-establish the database connection after connection loss. After each consecutive failure this interval is doubled, until maxReconnectInterval is reached. Successfully completing the connection establishment procedure resets the interval back to minReconnectInterval.

    The last parameter eventCallback can be set to a function which will be called by the Listener when the state of the underlying database connection changes. This callback will be called by the goroutine which dispatches the notifications over the Notify channel, so you should try to avoid doing potentially time-consuming operations from the callback.

    func (l *Listener) Close() error

    Close disconnects the Listener from the database and shuts it down. Subsequent calls to its methods will return an error. Close returns an error if the connection has already been closed.

    func (l *Listener) Listen(channel string) error

    Listen starts listening for notifications on a channel. Calls to this function will block until an acknowledgement has been received from the server. Note that Listener automatically re-establishes the connection after connection loss, so this function may block indefinitely if the connection can not be re-established.

    Listen will only fail in three conditions:

  • The channel is already open. The returned error will be ErrChannelAlreadyOpen.
  • The query was executed on the remote server, but PostgreSQL returned an error message in response to the query. The returned error will be a pq.Error containing the information the server supplied.
  • Close is called on the Listener before the request could be completed.
  • The channel name is case-sensitive.

    func (l *Listener) NotificationChannel() <-chan *Notification

    NotificationChannel returns the notification channel for this listener. This is the same channel as Notify, and will not be recreated during the life time of the Listener.

    func (l *Listener) Ping() error

    Ping the remote server to make sure it's alive. Non-nil return value means that there is no active connection.

    func (l *Listener) Unlisten(channel string) error

    Unlisten removes a channel from the Listener's channel list. Returns ErrChannelNotOpen if the Listener is not listening on the specified channel. Returns immediately with no error if there is no connection. Note that you might still get notifications for this channel even after Unlisten has returned.

    The channel name is case-sensitive.

    func (l *Listener) UnlistenAll() error

    UnlistenAll removes all channels from the Listener's channel list. Returns immediately with no error if there is no connection. Note that you might still get notifications for any of the deleted channels even after UnlistenAll has returned.

    func NewListenerConn(name string, notificationChan chan<- *Notification) (*ListenerConn, error)

    NewListenerConn creates a new ListenerConn. Use NewListener instead.

    func (l *ListenerConn) Err() error

    Err returns the reason the connection was closed. It is not safe to call this function until l.Notify has been closed.

    func (l *ListenerConn) ExecSimpleQuery(q string) (executed bool, err error)

    ExecSimpleQuery executes a "simple query" (i.e. one with no bindable parameters) on the connection. The possible return values are:

  • "executed" is true; the query was executed to completion on the database server. If the query failed, err will be set to the error returned by the database, otherwise err will be nil.
  • If "executed" is false, the query could not be executed on the remote server. err will be non-nil.
  • After a call to ExecSimpleQuery has returned an executed=false value, the connection has either been closed or will be closed shortly thereafter, and all subsequently executed queries will return an error.

    func (l *ListenerConn) Ping() error

    Ping the remote server to make sure it's alive. Non-nil error means the connection has failed and should be abandoned.

    const (
    	// ListenerEventConnected is emitted only when the database connection
    	// has been initially initialized. The err argument of the callback
    	// will always be nil.
    	ListenerEventConnected ListenerEventType = iota
    	// ListenerEventDisconnected is emitted after a database connection has
    	// been lost, either because of an error or because Close has been
    	// called. The err argument will be set to the reason the database
    	// connection was lost.
    	ListenerEventDisconnected
    	// ListenerEventReconnected is emitted after a database connection has
    	// been re-established after connection loss. The err argument of the
    	// callback will always be nil. After this event has been emitted, a
    	// nil pq.Notification is sent on the Listener.Notify channel.
    	ListenerEventReconnected
    	// ListenerEventConnectionAttemptFailed is emitted after a connection
    	// to the database was attempted, but failed. The err argument will be
    	// set to an error describing why the connection attempt did not
    	// succeed.
    	ListenerEventConnectionAttemptFailed
          
    type NewGSSFunc func() (GSS, error)

    NewGSSFunc creates a GSS authentication provider, for use with RegisterGSSProvider.

    type NoticeHandlerConnector struct {
    	driver.Connector
    	// contains filtered or unexported fields
      

    NoticeHandlerConnector wraps a regular connector and sets a notice handler on it.

    func ConnectorWithNoticeHandler(c driver.Connector, handler func(*Error)) *NoticeHandlerConnector

    ConnectorWithNoticeHandler creates or sets the given handler for the given connector. If the given connector is a result of calling this function previously, it is simply set on the given connector and returned. Otherwise, this returns a new connector wrapping the given one and setting the notice handler. A nil notice handler may be used to unset it.

    The returned connector is intended to be used with database/sql.OpenDB.

    Note: Notice handlers are executed synchronously by pq meaning commands won't continue to be processed until the handler returns.

    Example package main import ( "database/sql" "fmt" "log" "github.com/lib/pq" func main() { name := "" // Base connector to wrap base, err := pq.NewConnector(name) if err != nil { log.Fatal(err) // Wrap the connector to simply print out the message connector := pq.ConnectorWithNoticeHandler(base, func(notice *pq.Error) { fmt.Println("Notice sent: " + notice.Message) db := sql.OpenDB(connector) defer db.Close() // Raise a notice sql := "DO language plpgsql $$ BEGIN RAISE NOTICE 'test notice'; END $$" if _, err := db.Exec(sql); err != nil { log.Fatal(err)
    Output:
    Notice sent: test notice
          
    func (n *NoticeHandlerConnector) Connect(ctx context.Context) (driver.Conn, error)

    Connect calls the underlying connector's connect method and then sets the notice handler.

    type Notification struct {
    	// Process ID (PID) of the notifying postgres backend.
    	BePid int
    	// Name of the channel the notification was sent on.
    	Channel string
    	// Payload, or the empty string if unspecified.
    	Extra string
      

    Notification represents a single notification from the database.

    type NotificationHandlerConnector struct {
    	driver.Connector
    	// contains filtered or unexported fields
      

    NotificationHandlerConnector wraps a regular connector and sets a notification handler on it.

    func ConnectorWithNotificationHandler(c driver.Connector, handler func(*Notification)) *NotificationHandlerConnector

    ConnectorWithNotificationHandler creates or sets the given handler for the given connector. If the given connector is a result of calling this function previously, it is simply set on the given connector and returned. Otherwise, this returns a new connector wrapping the given one and setting the notification handler. A nil notification handler may be used to unset it.

    The returned connector is intended to be used with database/sql.OpenDB.

    Note: Notification handlers are executed synchronously by pq meaning commands won't continue to be processed until the handler returns.

    func (n *NotificationHandlerConnector) Connect(ctx context.Context) (driver.Conn, error)

    Connect calls the underlying connector's connect method and then sets the notification handler.

    NullTime represents a time.Time that may be null. NullTime implements the sql.Scanner interface so it can be used as a scan destination, similar to sql.NullString.

    Package listen is a self-contained Go program which uses the LISTEN / NOTIFY mechanism to avoid polling the database while waiting for more work to arrive.
    Package listen is a self-contained Go program which uses the LISTEN / NOTIFY mechanism to avoid polling the database while waiting for more work to arrive.
    go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic. Learn more.