Vagrantfile for PostgreSQL on Alpine Linux

In this previous post, I explained how to set up a PostgreSQL database on Linux Alpine. To make it repeatable, I condensed the entire setup within a Vagrantfile with the Linux Containers (LXC) provider.

Vagrant allows the process to be repeatable, human-error free, and portable. Simply copy the contents below into your Vagrantfile and run the command vagrant up.

To use the psql utility for the local database, use the following command:

$ vagrant ssh -c "sudo su -c 'psql -h localhost -U postgres'"

One note is that I am using the isc/lxc-alpine-3.10 box. Typically, I’ll strive to use boxes from official sources, but as of this post there were only 6 community contributed Linux Alpine boxes for the LXC provider.

Without further ado, the Vagrantfile contents:

$setup_timezone = <<-SCRIPT
echo "Setting up timezone data"
sudo apk add --no-cache tzdata
sudo cp /usr/share/zoneinfo/Asia/Singapore /etc/localtime
sudo apk del tzdata
SCRIPT

$setup_postgres_db = <<-'SCRIPT' 
echo "Setting up PostgreSQL database" 
sudo apk add --no-cache postgresql 
sudo su -c "if [ -d /var/lib/postgresql/data ]; then rm -rf /var/lib/postgresql/data; fi" postgres
sudo su -c "mkdir /var/lib/postgresql/data" postgres
sudo su -c "initdb /var/lib/postgresql/data" postgres
sudo su -c "echo host all all 0.0.0.0/0 md5 >> /var/lib/postgresql/data/pg_hba.conf" postgres
sudo su -c "echo listen_addresses=\'*\' >> /var/lib/postgresql/data/postgresql.conf" postgres
sudo sed -i 's/\/run\/postgresql,\/tmp/\/tmp/' /var/lib/postgresql/data/postgresql.conf

sudo su -c "echo -e '\x23\x21/bin/bash' > /etc/local.d/postgres-custom.start"
sudo su -c "echo sudo su -c \'pg_ctl start -D /var/lib/postgresql/data\' postgres >> /etc/local.d/postgres-custom.start"
sudo su -c "echo -e '\x23\x21/bin/bash' > /etc/local.d/postgres-custom.stop"
sudo su -c "echo sudo su -c \'pg_ctl stop -D /var/lib/postgresql/data\' postgres >> /etc/local.d/postgres-custom.stop"
sudo chmod +x /etc/local.d/postgres-custom.start
sudo chmod +x /etc/local.d/postgres-custom.stop
sudo rc-update add local default
sudo openrc
SCRIPT

Vagrant.configure("2") do |config|
  #Box from: https://app.vagrantup.com/isc/boxes/lxc-alpine-3.10
  config.vm.box = "isc/lxc-alpine-3.10"
  config.vm.box_version = "6"
  config.vm.provision :shell, inline: $setup_timezone

  config.vm.define "postgres" do |postgres|
    postgres.vm.provision :shell, inline: $setup_postgres_db
  end
end

====

In case anyone is interested, here is the stdout from running vagrant up:

$ vagrant up
Bringing machine 'postgres' up with 'lxc' provider...
==> postgres: Importing base box 'isc/lxc-alpine-3.10'...
==> postgres: Checking if box 'isc/lxc-alpine-3.10' version '6' is up to date...
==> postgres: Setting up mount entries for shared folders...
    postgres: /vagrant => /home/user/project-folder
==> postgres: Starting container...
==> postgres: Waiting for machine to boot. This may take a few minutes...
    postgres: SSH address: 10.0.3.245:22
    postgres: SSH username: vagrant
    postgres: SSH auth method: private key
    postgres:
    postgres: Vagrant insecure key detected. Vagrant will automatically replace
    postgres: this with a newly generated keypair for better security.
    postgres:
    postgres: Inserting generated public key within guest...
    postgres: Removing insecure key from the guest if it's present...
    postgres: Key inserted! Disconnecting and reconnecting using new SSH key...
==> postgres: Machine booted and ready!
==> postgres: Running provisioner: shell...
    postgres: Running: inline script
    postgres: Setting up timezone data
    postgres: fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
    postgres: fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
    postgres: (1/1) Installing tzdata (2019c-r0)
    postgres: Executing busybox-1.30.1-r3.trigger
    postgres: OK: 112 MiB in 44 packages
    postgres: (1/1) Purging tzdata (2019c-r0)
    postgres: Executing busybox-1.30.1-r3.trigger
    postgres: OK: 108 MiB in 43 packages
==> postgres: Running provisioner: shell...
    postgres: Running: inline script
    postgres: Setting up PostgreSQL database
    postgres: fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
    postgres: fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
    postgres: (1/8) Installing db (5.3.28-r1)
    postgres: (2/8) Installing libsasl (2.1.27-r4)
    postgres: (3/8) Installing libldap (2.4.48-r0)
    postgres: (4/8) Installing libpq (11.7-r0)
    postgres: (5/8) Installing postgresql-client (11.7-r0)
    postgres: (6/8) Installing tzdata (2019c-r0)
    postgres: (7/8) Installing libxml2 (2.9.9-r3)
    postgres: (8/8) Installing postgresql (11.7-r0)
    postgres: Executing busybox-1.30.1-r3.trigger
    postgres: OK: 130 MiB in 51 packages
    postgres: The files belonging to this database system will be owned by user "postgres".
    postgres: This user must also own the server process.
    postgres:
    postgres: The database cluster will be initialized with locales
    postgres:   COLLATE:  C
    postgres:   CTYPE:    C.UTF-8
    postgres:   MESSAGES: C
    postgres:   MONETARY: C
    postgres:   NUMERIC:  C
    postgres:   TIME:     C
    postgres: The default database encoding has accordingly been set to "UTF8".
    postgres: The default text search configuration will be set to "english".
    postgres:
    postgres: Data page checksums are disabled.
    postgres: fixing permissions on existing directory /var/lib/postgresql/data ... ok
    postgres: creating subdirectories ...
    postgres: ok
    postgres: selecting default max_connections ...
    postgres: 100
    postgres: selecting default shared_buffers ...
    postgres: 128MB
    postgres: selecting default timezone ...
    postgres: Singapore
    postgres: selecting dynamic shared memory implementation ... sysv
    postgres: creating configuration files ...
    postgres: ok
    postgres: running bootstrap script ...
    postgres: ok
    postgres: performing post-bootstrap initialization ...
    postgres: sh: locale: not found
    postgres: 2020-03-11 18:25:10.028 +08 [480] WARNING:  no usable system locales were found
    postgres: ok
    postgres: syncing data to disk ...
    postgres: ok
    postgres:
    postgres: WARNING: enabling "trust" authentication for local connections
    postgres: You can change this by editing pg_hba.conf or using the option -A, or
    postgres: --auth-local and --auth-host, the next time you run initdb.
    postgres:
    postgres: Success.
    postgres:  * service local added to runlevel default
    postgres:  * Caching service dependencies ... [ ok ]
    postgres:  * Starting local ...
    postgres:  [ ok ]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s