
Potsgres Setup
We use Postgres as our relational database. In this exercise, you will learn how to set up Postgres on your Digital Ocean Droplet. Later we will connect it to the hotelAPI Javalin application.
Introduction - flashback to 2nd semester
On 2nd semester we installed Postgres on our Digital Ocean Droplet. We used a docker-compose.yml file to set up the Postgres database. The file looked roughly like this:
# db: Postgresql data base server som kører på port 5432
version: '3'
services:
db:
image: postgres:16.2
container_name: db
restart: unless-stopped
networks:
- backend
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
volumes:
- ./data:/var/lib/postgresql/data/
- ./db/init.sql:/docker-entrypoint-initdb.d/init.sql
ports:
- "5432:5432"
networks:
backend:
name: backend
If you went through that setup on 2nd semester, as most did, you will have this compose file already on your Droplet. The docker-compose file is located in the ~jetty/2semDockerSetupRemote folder. If you don’t have it, then follow the 2nd semester tutorial and get on track.
Step 1: Update the Docker Compose File
We don’t want to loose our data, so we will keep the db service running on the backend network. This is the network that the hotelAPI service will connect to later. For security reasons, we will add environment variables for the Postgres user and password and store them in a an external .env file:
- Navigate to the
~jetty/2semDockerSetupRemotefolder on your Droplet. - Open the
docker-compose.ymlfile in an editor (nano). -
Update the file with the following content:
version: '3' services: db: image: postgres:16.2 container_name: db restart: unless-stopped networks: - backend environment: POSTGRES_USER: ${POSTGRES_USER} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} volumes: - ./data:/var/lib/postgresql/data/ - ./db/init.sql:/docker-entrypoint-initdb.d/init.sql ports: - "5432:5432" healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 30s timeout: 10s retries: 5 start_period: 10s networks: backend: name: backend -
It’s close to the 2nd semester version, but we have removed the
mem_limitandmem_reservationsettings. We have also added${POSTGRES_USER}and${POSTGRES_PASSWORD}as environment variables. These will be set in the.envfile later. We have also added a healthcheck to the service, that we will use later. - Save the file and exit the editor.
Step 2: Create the .env file
-
Create a
.envfile in the~jetty/2semDockerSetupRemotefolder:nano .env -
Add the following content to the file:
POSTGRES_USER=postgres POSTGRES_PASSWORD=<dit_sikre_password>Replace
<dit_sikre_password>with a secure password. -
Save the file and exit the editor.
Step 3: Start the Postgres Service
-
Start the Postgres service with Docker Compose:
docker-compose up -d -
Check that the service is running:
docker psYou should see the
dbservice running. -
Check the logs from the service:
docker logs db
Next step
Now that the Postgres service is running, we can connect the hotelAPI service to it. Follow the Hotel API exercise to set up the hotelAPI Javalin web application on your Digital Ocean Droplet.