Skip to content

Instantly share code, notes, and snippets.

@Jarmos-san
Last active June 28, 2024 00:31
Show Gist options
  • Save Jarmos-san/35de95a5da1d97bbd03ca6a0bc7550ce to your computer and use it in GitHub Desktop.
Save Jarmos-san/35de95a5da1d97bbd03ca6a0bc7550ce to your computer and use it in GitHub Desktop.
Install PostgreSQL using `cloud-init`

How to Install PostgreSQL Using cloud-init

cloud-init allows customising and configuring deployed server instances on most popular cloud service providers easily without requiring the use of other related tools like Ansible. Since cloud-init is such a reliable tool, this brief write-up will document the exact steps and procedures to install and setup PostgreSQL on a Debian 12 (Bookworm) server instance.

Get the Key ID From the Public Key

wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc \
  | gpg --with-fingerprint --with-colons \
  | awk -F: '/^fpr/ { print $10 }'
gpg --keyserver=keyserver.ubuntu.com --recv-keys <INSERT-KEY-ID-HERE>

Copy the cloud-config Contents

Then copy the following contents to your "user-data" or "cloud-config" file or UI/UX on the portal.

#cloud-config

package_update: true
package_upgrade: true

groups:
  - admin-group
    - root
  - ssh-users

users:
  - default
  - name: <INSERT-SSH-LOGIN-NAME>
    gecos: <INSERT-SSH-ID-NAME-AND-EMAIL>
    shell: /bin/bash
    lock_passwd: true
    group:
      - ssh-users
      - sudo
    sudo:
      - ALL=(ALL) NOPASSWD:ALL
    ssh_authorized_keys:
      - ssh-ed25519 AAAAC3NzaC1lZDI1NT<snip>

apt:
  sources:
    postgresql:
      keyid: <INSERT-KEY-ID-HERE>
      source: deb https://apt.postgresql.org/pub/repos/apt bookworm-pgdg main

packages:
  - postgresql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment