Documentation
¶
Overview ¶
Package database provides PostgreSQL database configuration and connection management. It supports environment-based configuration, DSN parsing, and connection pooling using the pgx driver for optimal performance and reliability.
Example usage:
// Create database connection from environment variables
ctx := context.Background()
db, err := database.NewFromEnv(ctx)
if err != nil {
log.Fatal(err)
}
defer db.Close(ctx)
// Create configuration from environment
config := database.NewConfigFromEnv()
connectionURL := config.ConnectionURL()
// Create configuration from DSN
config, err := database.NewFromDSN("postgres://user:pass@localhost/mydb")
if err != nil {
log.Fatal(err)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Name string // Database name
User string // Database user
Host string // Database host
Port string // Database port
SSLMode string // SSL mode (disable, require, verify-ca, verify-full)
ConnectionTimeout int // Connection timeout in seconds
Password string // Database password
SSLCertPath string // Path to SSL certificate file
SSLKeyPath string // Path to SSL key file
SSLRootCertPath string // Path to SSL root certificate file
PoolMinConnections string // Minimum connections in pool
PoolMaxConnections string // Maximum connections in pool
PoolMaxConnLife time.Duration // Maximum connection lifetime
PoolMaxConnIdle time.Duration // Maximum connection idle time
PoolHealthCheck time.Duration // Health check period for connections
}
Config holds database connection configuration parameters. It supports PostgreSQL connection settings including SSL, connection pooling, and timeout configurations for production database deployments.
func NewConfigFromEnv ¶
func NewConfigFromEnv() *Config
NewConfigFromEnv creates a new database configuration from environment variables. It loads configuration from .env file if present and supports both individual environment variables and a complete DSN via DB_DSN. If DB_DSN is provided, it takes precedence over individual variables. Default values are applied for connection pool settings when not specified.
func NewFromDSN ¶
NewFromDSN creates a new database configuration from a PostgreSQL DSN (Data Source Name). The DSN should be in the format: postgres://user:password@host:port/database?options Returns an error if the DSN cannot be parsed by the pgx driver.
func (*Config) ConnectionURL ¶
ConnectionURL generates a PostgreSQL connection URL from the configuration. Returns a properly formatted postgres:// URL with all configured parameters. Returns an empty string if the configuration is nil.
func (*Config) DatabaseConfig ¶
DatabaseConfig returns the database configuration. This method provides a consistent interface for accessing configuration settings.
type DB ¶
DB represents a database connection with connection pooling. It wraps a pgxpool.Pool to provide high-performance PostgreSQL connectivity with automatic connection management and health checking.
func NewFromEnv ¶
NewFromEnv creates a new database connection using environment configuration. It automatically configures connection pooling, health checks, and connection validation. The context is used for connection establishment and should have appropriate timeout. Returns an error if the configuration is invalid or connection fails.