config

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package config loads pgtower's configuration: a list of named connections plus global settings, from config.yml and the environment. Precedence, highest first: environment variables > config.yml > built-in defaults.

config.yml is owned by pgtower from format version 2 on: the Connections screen saves to it. Older (v1, single-connection) files are migrated in place on first run — see migrate.go.

Index

Constants

View Source
const (
	TagNone    = ""
	TagDev     = "dev"
	TagStaging = "staging"
	TagProd    = "prod"
)

Connection tags. Prod connections are highlighted in the header.

View Source
const EnvConnectionName = "env"

EnvConnectionName names the session-only connection built from DATABASE_URL or PG* variables. It is never written to config.yml.

View Source
const FileVersion = 2

FileVersion is the config.yml format written by this build.

Variables

Tags lists the valid connection tags, in display order.

Functions

func DefaultUser added in v0.11.0

func DefaultUser(host string) string

DefaultUser is the role used when a connection leaves the user empty: over a unix socket the OS user, as psql does (the only role peer authentication admits); over TCP "postgres".

func Env

func Env(name string) string

Env returns the PGTOWER_<name> variable, falling back to the pre-v0.10 PGTUI_<name> spelling.

func LegacyEnv

func LegacyEnv() []string

LegacyEnv lists the PGTUI_* variables set in the environment, so the UI can suggest renaming them.

func UserDir added in v0.12.0

func UserDir() string

UserDir is where pgtower keeps the user's config.yml and always saves it: $XDG_CONFIG_HOME/pgtower, else ~/.config/pgtower — on macOS too, so the path is the same everywhere. "" when the home directory is unknown.

Types

type Config

type Config struct {
	// Name and Tag identify the connection (header, prod highlight).
	Name string
	Tag  string

	// URL is the base DSN (postgres://user:pass@host:port/db?sslmode=...).
	// The database name is swapped at runtime when navigating between databases.
	URL string

	// AdminDB is the database used for cluster-level queries
	// (pg_database, pg_stat_activity, replication). Derived from the URL.
	AdminDB string

	// Host/Port only for display in the TUI header.
	Host string
	Port string
	User string

	// RefreshSeconds controls the dashboard auto-refresh.
	RefreshSeconds int

	// SCRAMIterations is the PBKDF2 round count for password resets. 0 means
	// "use the built-in default"; the db layer clamps it to a safe range.
	SCRAMIterations int

	// HostRAMMB and HostCPUs describe the server host (Postgres can't report
	// them via SQL). 0 = unknown; the tuning advisor then shows relative checks
	// only.
	HostRAMMB int
	HostCPUs  int

	// UpdateCheck enables the startup "a newer release is available" prompt.
	UpdateCheck bool

	// Version is the binary version (injected in main via -ldflags), shown
	// in the header. Filled in by whoever builds the Config.
	Version string
}

Config is the resolved, flat configuration of the active connection. The UI tabs consume it.

type Connection

type Connection struct {
	Name        string `yaml:"name"`
	URL         string `yaml:"url,omitempty"`
	Host        string `yaml:"host,omitempty"` // hostname, IP, or a unix-socket directory (/var/run/postgresql)
	Port        int    `yaml:"port,omitempty"`
	User        string `yaml:"user,omitempty"`
	Password    string `yaml:"password,omitempty"`
	PasswordEnv string `yaml:"password_env,omitempty"` // read the password from this variable instead
	Database    string `yaml:"database,omitempty"`
	SSLMode     string `yaml:"sslmode,omitempty"`
	Tag         string `yaml:"tag,omitempty"`
	// Per-server overrides of the global tuning-advisor host facts.
	HostRAMMB int `yaml:"host_ram_mb,omitempty"`
	HostCPUs  int `yaml:"host_cpus,omitempty"`
}

Connection is one named Postgres server. Either URL or the individual parts are set; the form in the Connections screen always writes parts.

func (Connection) DSN

func (c Connection) DSN() (string, error)

DSN builds the connection URL, filling in the password from PasswordEnv when set.

func (Connection) Parts

func (c Connection) Parts() Connection

Parts returns the connection as individual fields, decomposing URL when that is how it was stored (the form edits parts).

func (Connection) Validate

func (c Connection) Validate() error

Validate checks a connection before it is saved.

type Migration

type Migration struct {
	From     []string // what was migrated, e.g. "config.yml (v1)", ".env"
	Path     string   // the config.yml now in use
	Backups  []string // originals, kept (0600) next to the new file
	Imported []string // connection names created
	// Err is set when the upgraded file could not be written; pgtower then runs
	// from the in-memory upgrade and retries on the next save.
	Err error

	// Moved lists pgtower-era config directories moved to their pgtower names;
	// MoveErr is set when one could not be moved (it is still read in place).
	Moved   []string
	MoveErr error

	// Relocated lists config moved to UserDir ("from → to"): this user's
	// config.yml out of /opt/pgtower, or the macOS ~/Library directory.
	// RelocateErr is set when that failed or was left half done.
	Relocated   []string
	RelocateErr error
}

Migration describes a legacy configuration upgraded during Load. The UI shows it once, on the run that performed it.

type Store

type Store struct {
	// Path is the config.yml this store was read from and saves to. Empty until
	// the first save when no file existed.
	Path string

	Default     string
	Connections []Connection

	// Env is the connection from DATABASE_URL / PG* variables, if any. It wins
	// at startup (env > file) but lives only for this process.
	Env *Connection

	RefreshSeconds  int
	SCRAMIterations int
	HostRAMMB       int
	HostCPUs        int
	UpdateCheck     bool

	// Migration is set when Load upgraded a legacy config during this run.
	Migration *Migration

	// Skipped lists config files the search passed over because this user may
	// not read them (typically another user's 0600 file).
	Skipped []string
	// contains filtered or unexported fields
}

Store is the loaded configuration: every known connection and the global settings. It is what the Connections screen edits and saves.

func Load

func Load() (*Store, error)

Load finds config.yml (migrating a legacy one), then layers the environment on top. A missing file is not an error: the store is simply empty and the UI asks for a first connection.

func (*Store) Find

func (s *Store) Find(name string) (Connection, bool)

Find returns the connection with the given name (the env one included).

func (*Store) Initial

func (s *Store) Initial(explicit string) (Connection, bool, error)

Initial picks the connection to open at startup: an explicit name (-s), then the environment, then the saved default, then the only one. ok=false means the user has to choose (or create) one.

func (*Store) Names

func (s *Store) Names() []string

Names lists every connection name, the env one first.

func (*Store) Remove

func (s *Store) Remove(name string)

Remove deletes a connection (and clears it as default).

func (*Store) Resolve

func (s *Store) Resolve(c Connection, version string) (*Config, error)

Resolve turns a connection into the flat Config the UI runs with.

func (*Store) Save

func (s *Store) Save() error

Save writes the store to config.yml (format 2, mode 0600 — it may hold passwords): back where it was read from, except that a file read from a system directory (admin-managed) is saved to UserDir, which then takes precedence. With no file yet, it goes to UserDir.

func (*Store) SavePath added in v0.12.0

func (s *Store) SavePath() string

SavePath is the file Save writes.

func (*Store) Upsert

func (s *Store) Upsert(oldName string, c Connection) error

Upsert adds a connection or replaces the one named oldName.

Jump to

Keyboard shortcuts

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