PG_AUTOCTL CREATE POSTGRES(1)

PG_AUTOCTL CREATE POSTGRES(1) ๐Ÿ’–

okay bestie, this is the manual for pg_autoctl create postgres โ€” part of pg_auto_failover, and it's giving main character energy for your whole Postgres cluster. let's get into it โœจ


๐ŸŒธ NAME ๐ŸŒธ

pg_autoctl create postgres โ€” pg_autoctl create postgres

pg_autoctl create postgres โ€” initialize a pg_auto_failover postgres node ๐Ÿซถ


โœจ SYNOPSIS โœจ

so the command pg_autoctl create postgres initializes a standalone Postgres node into a pg_auto_failover monitor. and then? the monitor handles auto-failover for this Postgres node โ€” but only as soon as a secondary has been registered too, and is known to be healthy. no secondary, no failover, periodt. ๐Ÿ’…

usage: pg_autoctl create postgres

  --pgctl           path to pg_ctl
  --pgdata          path to data directory
  --pghost          PostgreSQL's hostname
  --pgport          PostgreSQL's port number
  --listen          PostgreSQL's listen_addresses
  --username        PostgreSQL's username
  --dbname          PostgreSQL's database name
  --name            pg_auto_failover node name
  --hostname        hostname used to connect from the other nodes
  --formation       pg_auto_failover formation
  --monitor         pg_auto_failover Monitor Postgres URL
  --auth            authentication method for connections from monitor
  --skip-pg-hba     skip editing pg_hba.conf rules
  --pg-hba-lan      edit pg_hba.conf rules for --dbname in detected LAN
  --ssl-self-signed setup network encryption using self signed certificates (does NOT protect against MITM)
  --ssl-mode        use that sslmode in connection strings
  --ssl-ca-file     set the Postgres ssl_ca_file to that file path
  --ssl-crl-file    set the Postgres ssl_crl_file to that file path
  --no-ssl          don't enable network encryption (NOT recommended, prefer --ssl-self-signed)
  --server-key      set the Postgres ssl_key_file to that file path
  --server-cert     set the Postgres ssl_cert_file to that file path
  --candidate-priority    priority of the node to be promoted to become primary
  --replication-quorum    true if node participates in write quorum
  --maximum-backup-rate   maximum transfer rate of data transferred from the server during initial sync

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

this command supports three different modes of initialization, matching that many implementation strategies. (yes, three. and then the list has four entries. we love a plot twist ๐Ÿ™ˆ but fr fr, read on.)

  1. Initialize a primary node from scratch ๐Ÿ‘‘

this happens when --pgdata (or the environment variable PGDATA) points to a non-existing or empty directory. then the given --hostname is registered to the pg_auto_failover --monitor as a member of the --formation.

the monitor answers the registration call with a state to assign to the new member of the group, either SINGLE or WAIT_STANDBY. when the assigned state is SINGLE, then pg_autoctl create postgres proceeds to initialize a new PostgreSQL instance. iconic.

  1. Initialize an already existing primary server ๐ŸŒŸ

this one hits when --pgdata (or the environment variable PGDATA) points to an already existing directory that belongs to a PostgreSQL instance. the standard PostgreSQL tool pg_controldata is used to recognize whether the directory belongs to a PostgreSQL instance.

in that case, the given --hostname is registered to the monitor in the tentative SINGLE state. when the given --formation and --group is currently empty, then the monitor accepts the registration and the pg_autoctl create prepares the already existing primary server for pg_auto_failover. slay.

  1. Initialize a secondary node from scratch ๐Ÿงš

this happens when --pgdata (or the environment variable PGDATA) points to a non-existing or empty directory, and when the monitor registration call assigns the state WAIT_STANDBY in step 1.

in that case, the pg_autoctl create command steps through the initial states of registering a secondary server, which includes preparing the primary server PostgreSQL HBA rules and creating a replication slot.

when the command ends successfully, a PostgreSQL secondary server has been created with pg_basebackup and is now started, catching-up to the primary server. ๐Ÿƒโ™€๏ธ๐Ÿ’จ

  1. Initialize a secondary node from an existing data directory ๐Ÿ“

when the data directory pointed to by the option --pgdata or the environment variable PGDATA already exists, then pg_auto_failover verifies that the system identifier matches the one of the other nodes already existing in the same group.

the system identifier can be obtained with the command pg_controldata. all nodes in a physical replication setting must have the same system identifier, and so in pg_auto_failover all the nodes in a same group have that constraint too. no cap.

when the system identifier matches the already registered system identifier of other nodes in the same group, then the node is set-up as a standby and Postgres is started with the primary conninfo pointed at the current primary. ๐Ÿ’œ


the --auth option allows setting up authentication method to be used when monitor node makes a connection to data node with pgautofailover_monitor user. as with the pg_autoctl create monitor command, you could use --auth trust when playing with pg_auto_failover at first and consider something production grade later. also, consider using --skip-pg-hba if you already have your own provisioning tools with a security compliance process.

see Security settings for pg_auto_failover for notes on .pgpass. ๐Ÿ”ฅ

โœจ OPTIONS โœจ

Okay bestie, here's the whole menu of things you can tell pg_autoctl create postgres to do. Every single one, no skipping ๐Ÿ’…

  • --pgctl โ€” the path to the pg_ctl tool for whichever PostgreSQL version you're vibing with.
    • If there's literally just one pg_ctl entry in your PATH, it defaults to that one. Wanna check your setup? Run which -a pg_ctl and see what comes back ๐Ÿ‘€
    • On RPM-based distros like RHEL or CentOS, the path is usually /usr/pgsql-13/bin/pg_ctl for Postgres 13.
    • On debian-based distros like debian or ubuntu, it's usually /usr/lib/postgresql/13/bin/pg_ctl for Postgres 13. Those distros also ship the package postgresql-common, which provides /usr/bin/pg_config โ€” and pg_autoctl can use that tool automatically to sniff out the default Postgres version on your setup. Iconic ๐ŸŽ€
  • --pgdata โ€” where to init a Postgres database cluster, using either pg_ctl initdb or pg_basebackup. Defaults to the environment variable PGDATA.
  • --pghost โ€” the hostname to use when connecting to the local Postgres instance from the pg_autoctl process. By default this field is left blank in the connection string, which lets you use Unix Domain Sockets with the default path compiled into your libpq version โ€” usually whatever your Operating System provides. On debian or ubuntu, that's /var/run/postgresql.
  • --pgport โ€” the Postgres port to use. Defaults to 5432.
  • --listen โ€” PostgreSQL's listen_addresses to set up. Heads up: right now only one address is supported in this command line option.
  • --username โ€” PostgreSQL's username to use when connecting to the local Postgres instance so it can be managed.
  • --dbname โ€” PostgreSQL's database name to use in your application. Defaults to being the same as --username, or to postgres when neither of those options is used.
  • --name โ€” the node name used on the monitor to refer to this node. Hostnames are, like, technical information, and given Postgres requirements around the HBA setup and DNS resolution (forward and reverse lookups), IP addresses often end up being used for the hostname.
    • So the --name option lets you use a user-friendly name for your Postgres nodes instead. Main character naming rights, honestly ๐Ÿ‘‘
  • --hostname โ€” the hostname or IP address (v4 and v6 both supported) to use from any other node to connect to this node.
    • When you don't provide it, a default value gets computed by running this algorithm:
      1. We grab this machine's "public IP" by opening a connection to the given monitor hostname or IP address. Then we get the TCP/IP client address that was used to make that connection.
      2. We then do a reverse DNS lookup on the IP address found in the previous step to fetch a hostname for our local machine.
      3. If the reverse DNS lookup is successful, then pg_autoctl does a forward DNS lookup of that hostname.
    • When the forward DNS lookup response in step 3 is an IP address found in one of our local network interfaces, then pg_autoctl uses the hostname found in step 2 as the default --hostname. Otherwise it uses the IP address found in step 1.
    • You can also just use the --hostname command line option to bypass this whole DNS lookup based process and force the local node name to a fixed value. Simple. Clean. Periodt โœจ
  • --formation โ€” the formation to register the node into on the monitor. Defaults to the default formation, which is automatically created in the monitor by the pg_autoctl create monitor command.
  • --monitor โ€” the Postgres URI used to connect to the monitor. It must use the autoctl_node username and target the pg_auto_failover database name. Fun fact: you can show the Postgres URI from the monitor node with the command pg_autoctl show uri.
  • --auth โ€” the authentication method pg_autoctl uses when editing the Postgres HBA file to open connections to other nodes. There's no default value, you have to provide one. And the value --trust? That's only a good choice for testing and evaluation of pg_auto_failover โ€” go read Security settings for pg_auto_failover for more. Don't be chaotic with this one, bestie ๐Ÿ™ˆ
  • --skip-pg-hba โ€” with this option, pg_autoctl refrains from any editing of the Postgres HBA file. But note: editing the HBA file is still needed so other nodes can connect using either read privileges or replication streaming privileges.
    • When --skip-pg-hba is used, pg_autoctl still outputs the HBA entries it needs in the logs โ€” it just skips editing the HBA file itself.
  • --pg-hba-lan โ€” with this option, pg_autoctl determines the local IP address used to connect to the monitor, retrieves its netmask, and uses that to compute your local area network CIDR. That CIDR then gets opened for connections in the Postgres HBA rules.
    • For example, when the monitor resolves to 192.168.0.1 and your local Postgres node uses an interface with IP address 192.168.0.2/255.255.255.0 to connect to the monitor, then the LAN CIDR is computed to be 192.168.0.0/24. Math ate, honestly ๐Ÿงฎ๐Ÿ’…
  • --candidate-priority โ€” sets this node's replication setting for candidate priority to the given value (between 0 and 100) at node registration on the monitor. Defaults to 50.
  • --replication-quorum โ€” sets this node's replication setting for replication quorum to the given value (either true or false) at node registration on the monitor. Defaults to true, which enables synchronous replication.
  • --maximum-backup-rate โ€” sets the maximum transfer rate of data transferred from the server during initial sync. This is used by pg_basebackup. Defaults to 100M.
  • --run โ€” immediately run the pg_autoctl service after having created this node.
  • --ssl-self-signed โ€” generate SSL self-signed certificates to provide network encryption. > Heads up: this does not protect against man-in-the-middle kinds of attacks. See Security settings for pg_auto_failover for more about the SSL settings.
  • --ssl-mode โ€” the SSL Mode used by pg_autoctl when connecting to other nodes, including when connecting for streaming replication.
  • --ssl-ca-file โ€” set the Postgres ssl_ca_file to that file path.
  • --ssl-crl-file โ€” set the Postgres ssl_crl_file to that file path.
  • --no-ssl โ€” don't enable network encryption. This is not recommended โ€” prefer --ssl-self-signed.
  • --server-key โ€” set the Postgres ssl_key_file to that file path.
  • --server-cert โ€” set the Postgres ssl_cert_file to that file path.

๐ŸŒˆ ENVIRONMENT ๐ŸŒˆ

The env vars are like the side quests that can replace some of the flags. Slay.

  • PGDATA โ€” Postgres directory location. Can be used instead of the --pgdata option.
  • PG_AUTOCTL_MONITOR โ€” Postgres URI to connect to the monitor node. Can be used instead of the --monitor option.
  • PG_AUTOCTL_NODE_NAME โ€” node name to register to the monitor. Can be used instead of the --name option.
  • PG_AUTOCTL_REPLICATION_QUORUM โ€” can be used instead of the --replication-quorum option.
  • PG_AUTOCTL_CANDIDATE_PRIORITY โ€” can be used instead of the --candidate-priority option.
  • PG_CONFIG โ€” can be set to the absolute path to the pg_config Postgres tool. This is mostly used in the context of building extensions, though it can also be a genuinely useful way to select a Postgres version when several are installed on the same system.
  • PATH โ€” used the usual way, mostly. Some entries that pg_autoctl searches for in the PATH are expected to be found only once, to avoid mistakes with Postgres major versions. (iykyk ๐Ÿซถ)
  • PGHOST, PGPORT, PGDATABASE, PGUSER, PGCONNECT_TIMEOUT, ... โ€” see the Postgres docs about Environment Variables for details.
  • TMPDIR โ€” the pgcopydb command creates all its work files and directories in ${TMPDIR}/pgcopydb, and defaults to /tmp/pgcopydb.
  • XDG_CONFIG_HOME โ€” the pg_autoctl command stores its configuration files in the standard place XDG_CONFIG_HOME. See the XDG Base Directory Specification.
  • XDG_DATA_HOME โ€” the pg_autoctl command stores its internal states files in the standard place XDG_DATA_HOME, which defaults to ~/.local/share. See the XDG Base Directory Specification.

๐ŸŽ€ AUTHOR ๐ŸŽ€

Microsoft

Copyright (c) Microsoft Corporation. All rights reserved.


And that's the whole thing, bestie โ€” you just read the 2.2 version, dated Apr 07, 2025, from the PG_AUTOCTL CREATE POSTGRES(1) manual. Go create some Postgres nodes, you absolute legend ๐Ÿ‘‘โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from