postgres

package
v2.7.1-0...-d77f34b Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2025 License: Apache-2.0 Imports: 18 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 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 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,
) (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, 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) Database

Database makes hba match connections made to a specific database.

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.

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) Replication

Replication makes hba match physical replication connections.

func (*HostBasedAuthentication) Role

Role makes hba match connections by users that are members of a specific role.

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

func (*HostBasedAuthentication) User

User makes hba match connections by a specific user.

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