postgres

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2025 License: MIT Imports: 17 Imported by: 0

README

PostgreSQL

Environment based configuration

Configuration for the PostgreSQL connection pool of the microservice.

  • POSTGRES_PORT default: 5432
    • Port to be used for listening used if address is not specified
  • POSTGRES_HOST default: localhost
    • Host where the PostgreSQL can be found (dns or IP)
  • POSTGRES_PASSWORD default: pace1234!
    • password to access the database
  • POSTGRES_USER default: postgres
    • postgres user to access the database
  • POSTGRES_DB default: postgres
    • database to access
  • POSTGRES_DIAL_TIMEOUT default: 5s
    • Dial timeout for establishing new connections
  • POSTGRES_READ_TIMEOUT default: 30s
    • Timeout for socket reads. If reached, commands will fail with a timeout instead of blocking
  • POSTGRES_WRITE_TIMEOUT default: 30s
    • Timeout for socket writes. If reached, commands will fail with a timeout instead of blocking.
  • POSTGRES_HEALTH_CHECK_TABLE_NAME default: healthcheck
    • Name of the Table that is created to try if database is writeable
  • POSTGRES_HEALTH_CHECK_RESULT_TTL default: 10s
    • Amount of time to cache the last health check result

Metrics

Prometheus metrics exposed.

  • pace_postgres_query_total{database} Collects stats about the number of postgres queries made
  • pace_postgres_query_failed{database} Collects stats about the number of postgres queries failed
  • pace_postgres_query_duration_seconds{database} Collects performance metrics for each postgres query
  • pace_postgres_query_affected_total{database} Collects stats about the number of rows affected by a postgres query
  • pace_postgres_connection_pool_hits{database} Collects number of times free connection was found in the pool
  • pace_postgres_connection_pool_misses{database} Collects number of times free connection was NOT found in the pool
  • pace_postgres_connection_pool_timeouts{database} Collects number of times a wait timeout occurred
  • pace_postgres_connection_pool_total_conns{database} Collects number of total connections in the pool
  • pace_postgres_connection_pool_idle_conns{database} Collects number of idle connections in the pool
  • pace_postgres_connection_pool_stale_conns{database} Collects number of stale connections removed from the pool

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotUnique = errors.New("not unique")

Functions

func IsErrConnectionFailed added in v0.4.5

func IsErrConnectionFailed(err error) bool

func NewDB added in v0.5.0

func NewDB(ctx context.Context, options ...ConfigOption) *bun.DB

Types

type Config added in v0.1.77

type Config struct {
	Port     int    `env:"POSTGRES_PORT" envDefault:"5432"`
	Host     string `env:"POSTGRES_HOST" envDefault:"postgres"`
	Password string `env:"POSTGRES_PASSWORD" envDefault:"mysecretpassword"`
	User     string `env:"POSTGRES_USER" envDefault:"postgres"`
	Database string `env:"POSTGRES_DB" envDefault:"postgres"`

	// ApplicationName is the application name. Used in logs on Pg side.
	// Only availaible from pg-9.0.
	ApplicationName string `env:"POSTGRES_APPLICATION_NAME" envDefault:"-"`
	// Dial timeout for establishing new connections.
	DialTimeout time.Duration `env:"POSTGRES_DIAL_TIMEOUT" envDefault:"5s"`
	// Name of the Table that is created to try if database is writeable
	HealthCheckTableName string `env:"POSTGRES_HEALTH_CHECK_TABLE_NAME" envDefault:"healthcheck"`
	// Amount of time to cache the last health check result
	HealthCheckResultTTL time.Duration `env:"POSTGRES_HEALTH_CHECK_RESULT_TTL" envDefault:"10s"`
	// Indicator whether write (insert,update,delete) queries should be logged
	LogWrite bool `env:"POSTGRES_LOG_WRITES" envDefault:"true"`
	// Indicator whether read (select) queries should be logged
	LogRead bool `env:"POSTGRES_LOG_READS" envDefault:"false"`
	// Timeout for socket reads. If reached, commands will fail
	// with a timeout instead of blocking.
	ReadTimeout time.Duration `env:"POSTGRES_READ_TIMEOUT" envDefault:"30s"`
	// Timeout for socket writes. If reached, commands will fail
	// with a timeout instead of blocking.
	WriteTimeout time.Duration `env:"POSTGRES_WRITE_TIMEOUT" envDefault:"30s"`
}

type ConfigOption added in v0.3.4

type ConfigOption func(cfg *Config)

func WithApplicationName added in v0.3.4

func WithApplicationName(applicationName string) ConfigOption

WithApplicationName -ApplicationName is the application name. Used in logs on Pg side. Only available from pg-9.0.

func WithDatabase added in v0.3.4

func WithDatabase(database string) ConfigOption

WithDatabase - customise the db name

func WithDialTimeout added in v0.3.4

func WithDialTimeout(dialTimeout time.Duration) ConfigOption

WithDialTimeout - Dial timeout for establishing new connections.

func WithHost added in v0.3.4

func WithHost(host string) ConfigOption

WithHost - customise the db host

func WithPassword added in v0.3.4

func WithPassword(password string) ConfigOption

WithPassword - customise the db password

func WithPort added in v0.3.4

func WithPort(port int) ConfigOption

WithPort - customize the db port

func WithQueryLogging added in v0.4.0

func WithQueryLogging(logRead, logWrite bool) ConfigOption

func WithReadTimeout added in v0.3.4

func WithReadTimeout(readTimeout time.Duration) ConfigOption

WithReadTimeout - Timeout for socket reads. If reached, commands will fail with a timeout instead of blocking.

func WithUser added in v0.3.4

func WithUser(user string) ConfigOption

WithUser - customise the db user

func WithWriteTimeout added in v0.3.4

func WithWriteTimeout(writeTimeout time.Duration) ConfigOption

WithWriteTimeout - Timeout for socket writes. If reached, commands will fail with a timeout instead of blocking.

type HealthCheck added in v0.1.15

type HealthCheck struct {
	// contains filtered or unexported fields
}

HealthCheck checks the state of a postgres connection. It must not be changed after it was registered as a health check.

func NewHealthCheck added in v0.5.0

func NewHealthCheck(db *bun.DB) *HealthCheck

NewHealthCheck creates a new HealthCheck instance.

func (*HealthCheck) CleanUp added in v0.1.15

func (h *HealthCheck) CleanUp(ctx context.Context) error

CleanUp drops the test table.

func (*HealthCheck) HealthCheck added in v0.1.15

HealthCheck performs the read test on the database. If enabled, it performs a write test as well.

func (*HealthCheck) Init added in v0.1.15

func (h *HealthCheck) Init(ctx context.Context) error

Init initializes the test table

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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