CodingNic

Build the Data Layer

Connect PostgreSQL

Build the Data Layer 12 min read

Connect PostgreSQL

Connect PostgreSQL

Readly needs a relational database before it can persist users, books, progress, and annotations.

Task

Start PostgreSQL locally and add its connection string to Readly’s environment.

Files

Work with:

text
docker-compose.yml
.env
.env.example

If the starter does not already contain a database service, add PostgreSQL to the local Compose setup.

A minimal service can look like:

yaml
services:
  postgres:
    image: postgres:16
    environment:
      POSTGRES_USER: readly
      POSTGRES_PASSWORD: readly
      POSTGRES_DB: readly
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

Add the corresponding local connection string to .env:

env
DATABASE_URL="postgresql://readly:readly@localhost:5432/readly"

Keep the same variable name in .env.example, but do not commit real credentials.

Start the Service

Run:

bash
docker compose up -d postgres

Then verify the container is running:

bash
docker compose ps

Test

The PostgreSQL container should report a running/healthy state, and Readly should still start with:

bash
npm run dev

Checkpoint

You are done when PostgreSQL is running locally and DATABASE_URL points Readly at it.