postgres

package
v0.1.36 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

db/postgres/postgres.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(connString string, timeout time.Duration) (*pgx.Conn, error)

Connect opens a single PostgreSQL connection using the given connection string and timeout. It performs a Ping to ensure the connection is usable before returning.

For production use with multiple concurrent requests, use ConnectPool instead.

The caller is responsible for calling conn.Close(ctx) when done.

Connection string examples:

"postgres://user:pass@localhost:5432/dbname"
"postgres://user:pass@localhost:5432/dbname?sslmode=disable"
"host=localhost port=5432 user=user password=pass dbname=dbname sslmode=disable"

func ConnectPool

func ConnectPool(connString string, timeout time.Duration) (*pgxpool.Pool, error)

ConnectPool opens a PostgreSQL connection pool using the given connection string and timeout. It performs a Ping to ensure the pool is usable before returning.

This is the recommended approach for production applications handling multiple concurrent requests. The pool automatically manages connection lifecycle, handles reconnection, and provides connection reuse.

The caller is responsible for calling pool.Close() when done.

Connection string examples:

"postgres://user:pass@localhost:5432/dbname"
"postgres://user:pass@localhost:5432/dbname?pool_max_conns=10"

func ConnectPoolWithConfig

func ConnectPoolWithConfig(config *pgxpool.Config, timeout time.Duration) (*pgxpool.Pool, error)

ConnectPoolWithConfig opens a PostgreSQL connection pool using the provided configuration. Use this when you need fine-grained control over pool settings.

The caller is responsible for calling pool.Close() when done.

Example:

config, _ := pgxpool.ParseConfig(connString)
config.MaxConns = 20
config.MinConns = 5
config.MaxConnLifetime = time.Hour
pool, err := postgres.ConnectPoolWithConfig(config, 10*time.Second)

func ParseConfig

func ParseConfig(connString string) (*pgxpool.Config, error)

ParseConfig parses a connection string into a pool configuration that can be modified before connecting. This is a convenience wrapper around pgxpool.ParseConfig.

Example:

config, err := postgres.ParseConfig("postgres://localhost/mydb")
if err != nil {
    return err
}
config.MaxConns = 20
pool, err := postgres.ConnectPoolWithConfig(config, 10*time.Second)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL