Skip to content

Digital Ocean

dragarcia edited this page Jul 26, 2021 · 21 revisions

Table of Contents

Quick install

1. Create your droplet

Head over to our Digital Ocean marketplace listing and create your droplet. You are free to create one even at the most basic configuration. No restrictions for you here.

2. Set up the password for the DB Superuser postgres

SSH into your instance using the user root and type in the following once you are in:

$ sudo -u postgres psql

This logs you into the psql terminal as the DB Superuser postgres.

psql Screenshot

Set your password with this command:

$ \password

Enter your desired password afterward and re-enter it when prompted. Once done, you can now exit your SSH connection.

3. Try out your new database

With any SQL client of your choice (Such as DBeaver), connect to your database with the following credentials:

  • Host: insert droplet ip address
  • Port: 5432
  • User: postgres
  • Password: insert your chosen password
  • Database: postgres

Build from scratch

Build

  • DO_TOKEN, REGION, SNAPSHOT_REGIONS all need to be defined. A list of valid Digital Ocean regions can be found here. For SNAPSHOT_REGIONS, regions are all contained in a single string, separated with a comma (eg. 'fra1,nyc1,sgp1').
$ export DO_TOKEN=your_digital_ocean_token
$ export REGION=your_chosen_region
$ export SNAPSHOT_REGIONS=your_chosen_snapshot_regions
  • ANSIBLE_ARGUMENTS by default excludes PgBouncer and PostgREST.
"--skip-tags,update-only,--skip-tags,install-postgrest,--skip-tags,install-pgbouncer,--skip-tags,install-supabase-internal"

If you want to install any of them into your image, set the value for ANSIBLE_ARGUMENTS while omitting the following parts:

# Remove this part to install PgBouncer
"--skip-tags,install-pgbouncer"

# Remove this part to install PostgREST
"--skip-tags,install-postgrest"

Create the Digital Ocean snapshot

$ packer build \
  -var "do_token=$DO_TOKEN" \
  -var "region=$REGION" \
  -var "snapshot_regions=$SNAPSHOT_REGIONS" \
  -var "ansible_arguments=$ANSIBLE_ARGUMENTS" \
  digitalOcean.json

Notes

To be in line with the standards of images found in the Digital Ocean Marketplace, scripts found in scripts are also ran to clean up the snapshot and make it compatible with the Marketplace. They are taken from here. More information on what these scripts achieve can be found here.

Build on top of an existing version

If you wish to build on top on an already existing version of Supabase Postgres, you need not build everything from scratch. You can circumvent the long build time. This utilises the tag feature of Ansible.

Making your changes

In the directory ansible, you can add new tasks in playbook.yml. You would then need to provide each of these new tasks with a tag for it to be identified. For this guide, we will use a tag named update.

Build

  • DO_TOKEN, REGION, SNAPSHOT_REGIONS, IMAGE_NAME and ARGS all need to be defined. A list of valid Digital Ocean regions can be found here. For SNAPSHOT_REGIONS, regions are all contained in a single string, separated with a comma (eg. 'fra1,nyc1,sgp1'). FOR IMAGE_NAME, the image name (supabase-supabasepostgres-18-04) of the marketplace version of Supabase Postgres should be used.
$ export DO_TOKEN=your_digital_ocean_token
$ export REGION=your_chosen_region
$ export SNAPSHOT_REGIONS=your_chosen_snapshot_regions
$ export IMAGE_NAME="supabase-supabasepostgres-18-04"
$ export ARGS="--tags,update"

Because we ultimately set the additional argument of --tags "update", we are signaling to Ansible to only run the tasks that have the tag update. Everything that has already been built before are skipped. You would need to make sure however that both versions in this repository and the marketplace image of Supabase Postgres are the same. You might be skipping crucial updates otherwise.

Create the Digital Ocean snapshot

$ packer build \
  -var "do_token=$DO_TOKEN" \
  -var "region=$REGION" \
  -var "snapshot_regions=$SNAPSHOT_REGIONS" \
  -var "image_name=$IMAGE_NAME" \
  -var "ansible_arguments=$ARGS" \
  digitalOcean.json

Attaching and using a volume

If you have a volume attached to your droplet, you can make the volume the default data directory for your Postgres server. Instructions provided are with reference to this guide.

1. Determine path for current data directory

$ sudo -u postgres psql

postgres=# SHOW data_directory;

For the current build of Supabase Postgres, this would return a path of /var/lib/postgresql/12/main.

2. Determine path for attached volume

$ ls /mnt/

An example path that we would use for this guide will be /mnt/volume_sgp1_01.

3. Shut down Postgres

$ sudo systemctl stop postgresql

4. Copy over the data directory over to the attached volume

$ sudo rsync -av /var/lib/postgresql /mnt/volume_sgp1_01

5. Backing up current data directory

$ sudo mv /var/lib/postgresql/12/main /var/lib/postgresql/12/main.bak

6. Point to the new data directory path

$ sudo vim /etc/postgresql/12/main/postgresql.conf

Inside the configuration, change the following:

...
data_directory = `/var/lib/postgresql/12/main`
...

to

...
data_directory = `/mnt/volume_sgp1_01/postgresql/12/main`
...

7. Restart Postgres

$ sudo systemctl start postgresql

8. Check if everything is working

  • Check if Postgres is up
$ sudo systemctl status postgresql
  • Check if the data directory path has been updated
$ sudo -u postgres psql
postgres=# SHOW data_directory;

At this point, this should return a path of /mnt/volume_sgp1_01/postgresql/12/main.

  • Verify the integrity of any existing data

9. Delete back up

$ sudo rm -rf /var/lib/postgresql/12/main.bak