postgres

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Default

func Default() *pgxpool.Pool

Default returns the default *pgxpool.Pool instance created by InitDefault.

func HealthChecker added in v0.1.19

func HealthChecker() health.Checker

HealthChecker returns a health.Checker that verifies PostgreSQL connectivity by pinging the default connection pool. The returned Checker reports health.StatusUnhealthy when no pool has been initialised or when the ping fails, and health.StatusHealthy when reachable.

Example

ExampleHealthChecker shows how to wire HealthChecker into a health.Check call for a /health HTTP endpoint. When the default pool has not been initialised, the Checker reports StatusUnhealthy.

package main

import (
	"context"
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlc/postgres"
	"github.com/phcp-tech/common-library-golang/health"
)

func main() {
	results := health.Check(context.Background(), postgres.HealthChecker())
	fmt.Println(results[0].Name)
	fmt.Println(results[0].Status == health.StatusUnhealthy) // true — Default() is nil
}
Output:
database
true

func InitDefault

func InitDefault(conf *Config) error

InitDefault initializes the default singleton PostgreSQL connection pool.

Example

ExampleInitDefault shows the singleton pattern for the default PostgreSQL pool. Call InitDefault once at application startup; subsequent calls are silently ignored (sync.Once).

In this example the singleton was already consumed by a prior test in this binary, so InitDefault is a no-op and returns nil. Default() returns nil because the first initialisation attempt failed (unreachable host).

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlc/postgres"
)

func main() {
	err := postgres.InitDefault(&postgres.Config{
		Host:            "127.0.0.1",
		Port:            "19999",
		Database:        "mydb",
		Username:        "user",
		Password:        "pass",
		MaxOpenConns:    10,
		MaxIdleConns:    2,
		ConnMaxLifetime: 60,
		ConnMaxIdletime: 10,
	})
	fmt.Println(err != nil)                // false — sync.Once no-op, returns nil
	fmt.Println(postgres.Default() != nil) // false — first init failed, instance nil
}
Output:
false
false

func NewPostgres

func NewPostgres(conf *Config) (*pgxpool.Pool, error)

NewPostgres opens a *pgxpool.Pool connection to PostgreSQL and configures the connection pool. It returns the pgx native pool so sqlc-generated Queries (sql_package: "pgx/v5") can use it directly.

Example

ExampleNewPostgres shows the typical usage of NewPostgres. NewPostgres performs an eager connectivity check (show search_path) so callers discover unreachable databases immediately at startup rather than on the first real query. Zero-value pool fields fall back to the dbsqlc package defaults (MaxOpenConns=100, MaxIdleConns=25, ConnMaxLifetime=60min, ConnMaxIdletime=10min).

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlc/postgres"
)

func main() {
	// With an unreachable host the connectivity check fails and an error is returned.
	_, err := postgres.NewPostgres(&postgres.Config{
		Host:     "127.0.0.1",
		Port:     "19999",
		Database: "mydb",
		Username: "user",
		Password: "pass",
	})
	fmt.Println(err != nil) // true — connectivity check failed
}
Output:
true
Example (CustomPool)

ExampleNewPostgres_customPool shows how to override the default connection pool settings.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbsqlc/postgres"
)

func main() {
	_, err := postgres.NewPostgres(&postgres.Config{
		Host:            "127.0.0.1",
		Port:            "19999",
		Database:        "mydb",
		Username:        "user",
		Password:        "pass",
		SearchPath:      "myschema",
		MaxOpenConns:    50,
		MaxIdleConns:    10,
		ConnMaxLifetime: 30, // minutes
		ConnMaxIdletime: 5,  // minutes
	})
	fmt.Println(err != nil) // true — connectivity check failed
}
Output:
true

Types

type Config

type Config struct {
	// Database connection parameters
	Host       string
	Port       string
	Database   string
	SearchPath string // optional
	Username   string
	Password   string

	// Connection pool parameters
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime int
	ConnMaxIdletime int
}

Directories

Path Synopsis
Package component provides PostgreSQL lifecycle integration for bootstrap.
Package component provides PostgreSQL lifecycle integration for bootstrap.

Jump to

Keyboard shortcuts

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