Help us shape the future of Go! Take the 2026 Go Developer Survey

postgres

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(conn *DBConn) (*gorm.DB, error)

New creates a new PostgreSQL database connection.

Types

type ConnectionConfig

type ConnectionConfig struct {
	Host     string `json:"host" yaml:"host"`
	Port     string `json:"port" yaml:"port"`
	UserName string `json:"username" yaml:"username"`
	Password string `json:"password" yaml:"password"`
}

ConnectionConfig defines the configuration for a single database connection.

func (*ConnectionConfig) DSN

func (c *ConnectionConfig) DSN(cfg *DBConn) string

DSN generates a PostgreSQL connection string.

type DBConn

type DBConn struct {
	// Primary configuration.
	Master ConnectionConfig `json:"master" yaml:"master"`

	// Replica configuration list.
	Replicas []ConnectionConfig `json:"replicas" yaml:"replicas"`

	// Connection pool settings.
	MaxIdleConns    int           `json:"maxIdleConns" yaml:"maxIdleConns"`
	MaxOpenConns    int           `json:"maxOpenConns" yaml:"maxOpenConns"`
	ConnMaxLifetime time.Duration `json:"connMaxLifetime" yaml:"connMaxLifetime"`

	// Database name.
	Database string `json:"database" yaml:"database"`

	// PostgreSQL-specific settings.
	Schema     string `json:"schema" yaml:"schema"`
	SearchPath string `json:"searchPath" yaml:"searchPath"`
	SSLMode    string `json:"sslMode" yaml:"sslMode"`

	// PostgreSQL timeout settings.
	StatementTimeout                time.Duration `json:"statementTimeout" yaml:"statementTimeout"`                               // statement_timeout
	LockTimeout                     time.Duration `json:"lockTimeout" yaml:"lockTimeout"`                                         // lock_timeout
	IdleInTransactionSessionTimeout time.Duration `json:"idleInTransactionSessionTimeout" yaml:"idleInTransactionSessionTimeout"` // idle_in_transaction_session_timeout

	// pgx-specific settings.
	ApplicationName string            `json:"applicationName" yaml:"applicationName"`
	RuntimeParams   map[string]string `json:"runtimeParams" yaml:"runtimeParams"`

	// Preset configuration for default connection behavior.
	// Optional values: "", PresetSupabaseTransaction.
	Preset Preset `json:"preset" yaml:"preset"`
	// GORM settings that override preset behavior.
	GORM *GORMConfig `json:"gorm" yaml:"gorm"`
	// PGX settings at the driver layer.
	PGX *PGXConfig `json:"pgx" yaml:"pgx"`
}

DBConn combines primary and replica configurations.

type GORMConfig added in v1.1.0

type GORMConfig struct {
	// Skip GORM default implicit transactions.
	// Useful for transaction poolers such as Supabase/pgBouncer.
	SkipDefaultTransaction *bool `json:"skipDefaultTransaction" yaml:"skipDefaultTransaction"`
	// Disable server-side prepared statements.
	// Useful for transaction poolers.
	PrepareStmt *bool `json:"prepareStmt" yaml:"prepareStmt"`
}

GORMConfig defines behavior settings at the GORM layer.

type PGXConfig added in v1.1.0

type PGXConfig struct {
	// Statement cache capacity; set to 0 to disable entirely.
	// Useful for transaction poolers.
	StatementCacheCap *int `json:"statementCacheCap" yaml:"statementCacheCap"`
}

PGXConfig defines settings at the pgx driver layer.

type Preset added in v1.1.0

type Preset string
const (
	PresetSupabaseTransaction Preset = "supabase_transaction"
)

Jump to

Keyboard shortcuts

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