postgres

package
v3.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package postgres is a collection of resources that interact with PostgreSQL or provide functionality that makes it easier for other resources to interact with PostgreSQL.

Index

Examples

Constants

View Source
const (

	// SocketDirectory is where to bind and connect to UNIX sockets.
	SocketDirectory = "/tmp/postgres"

	// ReplicationUser is the PostgreSQL role that will be created by Patroni
	// for streaming replication and for `pg_rewind`.
	ReplicationUser = "_crunchyrepl"
)
View Source
const (
	// IANAPortNumber is the port assigned to PostgreSQL at the IANA.
	IANAPortNumber = 5432

	// IANAServiceName is the name of the PostgreSQL protocol at the IANA.
	IANAServiceName = "postgresql"
)

The protocol used by PostgreSQL is registered with the Internet Assigned Numbers Authority (IANA). - https://www.iana.org/assignments/service-names-port-numbers

Variables

View Source
var RESERVED_SCHEMA_NAMES = map[string]bool{
	"public":    true,
	"pgbouncer": true,
	"monitor":   true,
}

Functions

func AdditionalConfigVolumeMount

func AdditionalConfigVolumeMount() corev1.VolumeMount

AdditionalConfigVolumeMount returns the name and mount path of the additional config files.

func ConfigDirectory

func ConfigDirectory(cluster *v1beta1.PostgresCluster) string

ConfigDirectory returns the absolute path to $PGDATA for cluster. - https://www.postgresql.org/docs/current/runtime-config-file-locations.html

func CreateDatabasesInPostgreSQL

func CreateDatabasesInPostgreSQL(
	ctx context.Context, exec Executor, databases []string,
) error

CreateDatabasesInPostgreSQL calls exec to create databases that do not exist in PostgreSQL.

func DataDirectory

func DataDirectory(cluster *v1beta1.PostgresCluster) string

DataDirectory returns the absolute path to the "data_directory" of cluster. - https://www.postgresql.org/docs/current/runtime-config-file-locations.html

func DataVolumeMount

func DataVolumeMount() corev1.VolumeMount

DataVolumeMount returns the name and mount path of the PostgreSQL data volume.

func DownwardAPIVolumeMount

func DownwardAPIVolumeMount() corev1.VolumeMount

DownwardAPIVolumeMount returns the name and mount path of the DownwardAPI volume.

func Environment

func Environment(cluster *v1beta1.PostgresCluster) []corev1.EnvVar

Environment returns the environment variables required to invoke PostgreSQL utilities.

func HugePages1GiRequested

func HugePages1GiRequested(cluster *v1beta1.PostgresCluster) bool

This helper function checks to see if a hugepages-1Gi value greater than zero has been set in any of the PostgresCluster's instances' resource specs

func HugePages2MiRequested

func HugePages2MiRequested(cluster *v1beta1.PostgresCluster) bool

This helper function checks to see if a hugepages-2Mi value greater than zero has been set in any of the PostgresCluster's instances' resource specs

func HugePagesRequested

func HugePagesRequested(cluster *v1beta1.PostgresCluster) bool

This helper function checks to see if a huge_pages value greater than zero has been set in any of the PostgresCluster's instances' resource specs

func InstancePod

func InstancePod(ctx context.Context,
	inCluster *v1beta1.PostgresCluster,
	inInstanceSpec *v1beta1.PostgresInstanceSetSpec,
	inClusterCertificates, inClientCertificates *corev1.SecretProjection,
	inDataVolume, inWALVolume *corev1.PersistentVolumeClaim,
	inTablespaceVolumes []*corev1.PersistentVolumeClaim,
	outInstancePod *corev1.PodSpec,
)

InstancePod initializes outInstancePod with the database container and the volumes needed by PostgreSQL.

func PGTDEDirectory

func PGTDEDirectory(cluster *v1beta1.PostgresCluster) string

PGTDEDirectory returns the absolute path to the directory where pg_tde keeps its keyring.

func PGTDELinkCommands

func PGTDELinkCommands(
	cluster *v1beta1.PostgresCluster, instance *v1beta1.PostgresInstanceSetSpec,
) []string

PGTDELinkCommands returns Bash statements that bring the pg_tde keyring links in line with the spec: with WAL encryption on they link the keyring into the root of the volume that holds the data directory and, when instance keeps its WAL files on a volume of their own, into the root of that volume as well; with WAL encryption off they take those links back out.

K8SPG-911: the tools that archive_command and restore_command run -- pg_tde_archive_decrypt, pg_tde_restore_encrypt and pg_tde_waldump -- are handed the path of a WAL segment and derive the keyring location from it. PostgreSQL writes WAL in "pg_wal" inside the data directory, but that is a symbolic link onto the volume that holds the WAL files here, so the tools end up looking for the keyring in the root of that volume.

func PGTDEVolume

func PGTDEVolume(vault *v1beta1.PGTDEVaultSpec) corev1.Volume

PGTDEVolume returns the projected volume for pg_tde Vault secrets (token and optional CA cert).

func PGTDEVolumeMount

func PGTDEVolumeMount() corev1.VolumeMount

PGTDEVolumeMount returns the name and mount path of the token and certificates for KMS.

func PodSecurityContext

func PodSecurityContext(cluster *v1beta1.PostgresCluster) *corev1.PodSecurityContext

PodSecurityContext returns a v1.PodSecurityContext for cluster that can write to PersistentVolumes.

func QuoteLiteral

func QuoteLiteral(v string) string

QuoteLiteral escapes v so it can be safely used as a literal (or constant) in an SQL statement.

func ReleaseIsFinal

func ReleaseIsFinal(majorVersion int, t time.Time) bool

ReleaseIsFinal returns whether or not t is definitively past the final scheduled release of a Postgres version.

func ResolveLogDirectory

func ResolveLogDirectory(dataDir, input string) (directory string, unsafe, relative bool)

ResolveLogDirectory returns the absolute path Postgres uses for "log_directory" when its "data_directory" is dataDir and "log_directory" is set to input. When input is not a safe value, it returns the absolute path to a good one instead and reports why: unsafe when input names a path Postgres controls, relative when input is a non-default relative path. Callers that derive paths which must agree with the running Postgres configuration (e.g. the log collector) should use this rather than duplicating the resolution.

https://www.postgresql.org/docs/current/runtime-config-logging.html#GUC-LOG-DIRECTORY

func SanitizeParameters

func SanitizeParameters(cluster *v1beta1.PostgresCluster, parameters *ParameterSet, recorder record.EventRecorder)

SanitizeParameters transforms parameters so they are safe for Postgres in cluster.

func SetHugePages

func SetHugePages(cluster *v1beta1.PostgresCluster, pgParameters *Parameters)

This function looks for a valid huge_pages resource request. If it finds one, it sets the PostgreSQL parameter "huge_pages" to "try". If it doesn't find one, it sets "huge_pages" to "off".

func TablespaceVolumeMount

func TablespaceVolumeMount(tablespaceName string) corev1.VolumeMount

TablespaceVolumeMount returns the name and mount path of the PostgreSQL tablespace data volume.

func WALDirectory

func WALDirectory(
	cluster *v1beta1.PostgresCluster, instance *v1beta1.PostgresInstanceSetSpec,
) string

WALDirectory returns the absolute path to the directory where an instance stores its WAL files. - https://www.postgresql.org/docs/current/wal.html

func WALStorage

func WALStorage(instance *v1beta1.PostgresInstanceSetSpec) string

WALStorage returns the absolute path to the disk where an instance stores its WAL files. Use WALDirectory for the exact directory that Postgres uses.

func WALVolumeMount

func WALVolumeMount() corev1.VolumeMount

WALVolumeMount returns the name and mount path of the PostgreSQL WAL volume.

func WriteUsersInPostgreSQL

func WriteUsersInPostgreSQL(
	ctx context.Context, cluster *v1beta1.PostgresCluster, exec Executor,
	users []v1beta1.PostgresUserSpec, verifiers map[string]string,
) error

WriteUsersInPostgreSQL calls exec to create users that do not exist in PostgreSQL. Once they exist, it updates their options and passwords and grants them access to their specified databases. The databases must already exist.

Types

type Executor

type Executor func(
	ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string,
) error

Executor provides methods for calling "psql".

Example (ExecCmd)

This example demonstrates how Executor can work with exec.Cmd.

_ = Executor(func(
	ctx context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string,
) error {
	// #nosec G204 Nothing calls the function defined in this example.
	cmd := exec.CommandContext(ctx, command[0], command[1:]...)
	cmd.Stdin, cmd.Stdout, cmd.Stderr = stdin, stdout, stderr
	return cmd.Run()
})

func (Executor) Exec

func (exec Executor) Exec(
	ctx context.Context, sql io.Reader, variables map[string]string,
	psqlArgs []string,
) (string, string, error)

Exec uses "psql" to execute sql. The sql statement(s) are passed via stdin and may contain psql variables that are assigned from the variables map. - https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-VARIABLES

func (Executor) ExecInAllDatabases

func (exec Executor) ExecInAllDatabases(
	ctx context.Context, sql string, variables map[string]string,
) (string, string, error)

ExecInAllDatabases uses "bash" and "psql" to execute sql in every database that allows connections, including templates. The sql command(s) may contain psql variables that are assigned from the variables map. - https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-VARIABLES

func (Executor) ExecInDatabasesFromQuery

func (exec Executor) ExecInDatabasesFromQuery(
	ctx context.Context, databases, sql string, variables map[string]string,
) (string, string, error)

ExecInDatabasesFromQuery uses "bash" and "psql" to execute sql in every database returned by the databases query. The sql statement(s) may contain psql variables that are assigned from the variables map. - https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-VARIABLES

type HBAs

type HBAs struct {
	Mandatory []*HostBasedAuthentication
	Default   []*HostBasedAuthentication
}

HBAs is a pairing of HostBasedAuthentication records.

func NewHBAs

func NewHBAs() HBAs

NewHBAs returns HostBasedAuthentication records required by this package.

type HostBasedAuthentication

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

HostBasedAuthentication represents a single record for pg_hba.conf. - https://www.postgresql.org/docs/current/auth-pg-hba-conf.html

func NewHBA

func NewHBA() *HostBasedAuthentication

NewHBA returns an HBA record that matches all databases, networks, and users.

func (*HostBasedAuthentication) AllDatabases

func (hba *HostBasedAuthentication) AllDatabases() *HostBasedAuthentication

AllDatabases makes hba match connections made to any database.

func (*HostBasedAuthentication) AllNetworks

AllNetworks makes hba match connection attempts made from any IP address.

func (*HostBasedAuthentication) AllUsers

AllUsers makes hba match connections made by any user.

func (*HostBasedAuthentication) Databases

func (hba *HostBasedAuthentication) Databases(name string, names ...string) *HostBasedAuthentication

Databases makes hba match connections made to specific databases.

func (*HostBasedAuthentication) Local

Local makes hba match connection attempts using Unix-domain sockets.

func (*HostBasedAuthentication) Method

Method specifies the authentication method to use when a connection matches hba. Method names are bare identifiers in pg_hba.conf and are not quoted.

func (*HostBasedAuthentication) Network

Network makes hba match connection attempts from a block of IP addresses in CIDR notation.

func (*HostBasedAuthentication) NoSSL

NoSSL makes hba match connection attempts made over TCP/IP without SSL.

func (*HostBasedAuthentication) Options

Options specifies any options for the authentication method.

func (*HostBasedAuthentication) Origin

func (*HostBasedAuthentication) Replication

Replication makes hba match physical replication connections.

func (*HostBasedAuthentication) SameNetwork

SameNetwork makes hba match connection attempts from IP addresses in any subnet to which the server is directly connected.

func (*HostBasedAuthentication) String

func (hba *HostBasedAuthentication) String() string

String returns hba formatted for the pg_hba.conf file without a newline.

func (*HostBasedAuthentication) TCP

TCP makes hba match connection attempts made using TCP/IP, with or without SSL.

func (*HostBasedAuthentication) TLS

TLS makes hba match connection attempts made using TCP/IP with TLS.

func (*HostBasedAuthentication) TLSOnly

TLSOnly changes the HBA record to require TLS. Records that already require TLS ("hostssl") or use Unix sockets ("local") are not modified. TCP records ("host") and no-SSL records ("hostnossl") become "hostssl".

func (*HostBasedAuthentication) Users

func (hba *HostBasedAuthentication) Users(name string, names ...string) *HostBasedAuthentication

Users makes hba match connections by specific users.

type OrderedHBAs

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

OrderedHBAs is an append-only sequence of pg_hba.conf lines.

func (*OrderedHBAs) Append

func (o *OrderedHBAs) Append(hbas ...*HostBasedAuthentication)

Append renders and adds pg_hba.conf lines to o. Nil pointers are ignored.

func (*OrderedHBAs) AppendUnstructured

func (o *OrderedHBAs) AppendUnstructured(hbas ...string)

AppendUnstructured trims and adds unvalidated pg_hba.conf lines to o. Empty lines and lines that are entirely control characters are omitted.

func (*OrderedHBAs) AsStrings

func (o *OrderedHBAs) AsStrings() []string

AsStrings returns a copy of o as a slice.

func (*OrderedHBAs) Length

func (o *OrderedHBAs) Length() int

Length returns the number of records in o.

type ParameterSet

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

ParameterSet is a collection of PostgreSQL parameters. - https://www.postgresql.org/docs/current/config-setting.html

func NewParameterSet

func NewParameterSet() *ParameterSet

NewParameterSet returns an empty ParameterSet.

func (*ParameterSet) Add

func (ps *ParameterSet) Add(name, value string)

Add sets parameter name to value.

func (*ParameterSet) AppendToList

func (ps *ParameterSet) AppendToList(name string, value ...string)

AppendToList adds each value to the right-hand side of parameter name as a comma-separated list without quoting.

func (*ParameterSet) AsMap

func (ps *ParameterSet) AsMap() map[string]string

AsMap returns a copy of ps as a map.

func (*ParameterSet) DeepCopy

func (ps *ParameterSet) DeepCopy() (out *ParameterSet)

DeepCopy returns a copy of ps.

func (*ParameterSet) Get

func (ps *ParameterSet) Get(name string) (string, bool)

Get returns the value of parameter name and whether or not it was present in ps.

func (*ParameterSet) Has

func (ps *ParameterSet) Has(name string) bool

Has returns whether or not parameter name is present in ps.

func (*ParameterSet) String

func (ps *ParameterSet) String() string

func (*ParameterSet) Value

func (ps *ParameterSet) Value(name string) string

Value returns empty string or the value of parameter name if it is present in ps.

type Parameters

type Parameters struct{ Mandatory, Default *ParameterSet }

Parameters is a pairing of ParameterSets.

func NewParameters

func NewParameters() Parameters

NewParameters returns ParameterSets required by this package.

Directories

Path Synopsis
package password lets one create the appropriate password hashes and verifiers that are used for adding the information into PostgreSQL
package password lets one create the appropriate password hashes and verifiers that are used for adding the information into PostgreSQL

Jump to

Keyboard shortcuts

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