Postgres Docker Setup

You can install Postgres under docker like:

# Create the network for mapping later
docker network create --label placecal-network
docker network create placecal-network

# Pull down the postgres image
docker pull postgres:13.4

# You can replace:
# POSGRES_USER
# POSTGRES_PASSWORD
# With secure-er variants, if you wish, but we will need to export them later
docker create --name postgres-placecal --network placecal-network --network-alias postgres -p 5432:5432 --health-cmd pg_isready --health-internal 10s --health-timeout 5s --health-retries 5 -e 'POSTGRES_DB=placecal_test' -e 'POSTGRES_USER=postgres' -e "POSTGRES_PASSWORD=foobar" -e 'POSTGRES_PORT=5432' postgres:13.4

The last (create) command will give us back a hash that represents that specific instance of postgres, we use that for any future commands, like starting it, inspecting it, etc.

# Now we start the docker command
docker start postgres-placecal

# To confirm that it is working we need to have a hash that represents the current instance of the container we can get this hash by looking at the first column of this command:
docker ps
# In this case, the container id was ba926b81b0f3

# So now we check that the health check came out positive, this confirms it is working:
docker ps --all --filter id=<hash> --filter status=running --no-trunc

# Visually inspect to see what port it's mapped to
docker port postgres-placecal

PlaceCal depends on the following environment variables to correctly connect to a dockerized postgres server, set them correctly including any changes that you made earlier:

POSTGRES_HOST=localhost
POSTGRES_USER=postgres
PGPASSWORD=foobar

Additionally when doing rake db:* tasks, such as importing from production, we require psql to also form a connection to the postgres server. This means that we need to set the following environment variables:

PGHOST=$POSTGRES_HOST
PHUSER=$POSTGRES_USER

Ideally eventually we should change the database.yml file specifically so that it uses those, instead, or namespaced variables like PLACECAL_POSTGRES_USER, etc. While insecure, I have all 5 of these set in my .bashrc for convenience' sake Finally, you will need to run docker start <create hash> whenever you reboot, or whenever you have stopped the postgres instance.