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
- Variables
- func DefaultUser(host string) string
- func Env(name string) string
- func LegacyEnv() []string
- func UserDir() string
- type Config
- type Connection
- type Migration
- type Store
- func (s *Store) Find(name string) (Connection, bool)
- func (s *Store) Initial(explicit string) (Connection, bool, error)
- func (s *Store) Names() []string
- func (s *Store) Remove(name string)
- func (s *Store) Resolve(c Connection, version string) (*Config, error)
- func (s *Store) Save() error
- func (s *Store) SavePath() string
- func (s *Store) Upsert(oldName string, c Connection) error
Constants ¶
const ( TagNone = "" TagDev = "dev" TagStaging = "staging" TagProd = "prod" )
Connection tags. Prod connections are highlighted in the header.
const EnvConnectionName = "env"
EnvConnectionName names the session-only connection built from DATABASE_URL or PG* variables. It is never written to config.yml.
const FileVersion = 2
FileVersion is the config.yml format written by this build.
Variables ¶
var Tags = []string{TagNone, TagDev, TagStaging, TagProd}
Tags lists the valid connection tags, in display order.
Functions ¶
func DefaultUser ¶ added in v0.11.0
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 ¶
Env returns the PGTOWER_<name> variable, falling back to the pre-v0.10 PGTUI_<name> spelling.
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 ¶
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) 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 ¶
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.