Search⌘ K
AI Features

Starting a Postgres Server

Explore how to add a Postgres database service to your Rails app with Docker Compose. Learn to configure environment variables securely, start the database in detached mode, and verify it is running, preparing your app for reliable data persistence.

Postgres server

We want to run a Postgres server for our Rails app to use. The process is very similar to adding Redis. In the previous chapter, we started by familiarizing ourselves with how to run the Redis server with docker run. However, now that we have had some experience with adding a service, we can jump straight to setting up Postgres directly with Compose.

Modification of docker-compose.yml

Let’s add Postgres to our docker-compose.yml file:

YAML
version: '3'
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- . :/usr/src/app
redis:
image: redis
database:
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: some-long-secure-password
POSTGRES_DB: myapp_development

Explanation

We have defined a new database service using the official postgres image. We are relying on this image’s default CMD ...