How to create persistent PostgreSQL database with docker-compose
To spin up Postgres on Docker, create docker-compose.yml
like so:
version: '3.3'
services:
db:
image: postgres:14-alpine # alpine images are lighweight
restart: always
container_name: db
ports:
- 5432:5432 # expose default Postgres port for connections
environment:
# initialize database connection parameters with environment variables.
# more info: <https://hub.docker.com/_/postgres>
POSTGRES_USER: me
POSTGRES_PASSWORD: password
POSTGRES_DB: mydb
volumes:
# persistent storage of data
- ./pg_data:/var/lib/postgresql/data
Finally in your terminal:
docker-compose up -d