Skip to main content
⚡ OpenDevDocs
Docker Recipes
devopsdockerRecipeIntermediate

Node.js + PostgreSQL with Docker Compose

Complete full-stack development environment setup connecting a Node.js Express/TypeScript backend with a containerized PostgreSQL database using Docker Compose.

Verified: 2026-09-30
Tested on:node 22-alpine, postgres 16-alpine, docker-compose v2.x
Platforms:
linuxmacoswindows
#nodejs#postgresql#docker-compose#devops#fullstack

Goal

Create a complete local development environment where a Node.js application connects to a PostgreSQL container over an isolated internal Docker bridge network with health checks and volume persistence.


Step-by-Step Implementation

```
├── Dockerfile
├── compose.yaml
├── package.json
├── .env
└── src/
    └── server.js
```
  api:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    environment:
      - PORT=3000
      - DATABASE_URL=postgresql://devuser:devpassword@db:5432/app_development
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - ./src:/app/src
    networks:
      - internal-net

volumes:
  pg_data:

networks:
  internal-net:
    driver: bridge
```
const app = express();
const port = process.env.PORT || 3000;

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
});

app.get('/api/users', async (req, res) => {
  try {
    const { rows } = await pool.query('SELECT NOW() as current_time');
    res.json({ success: true, server_time: rows[0].current_time });
  } catch (err) {
    console.error('Database query error:', err);
    res.status(500).json({ error: 'Database connection failed' });
  }
});

app.listen(port, () => {
  console.log(`API running on http://localhost:${port}`);
});
```
# View live logs
docker compose logs -f
```

Verification

Test your API endpoint from the host terminal:

curl http://localhost:3000/api/users

Response:

{
  "success": true,
  "server_time": "2026-09-30T12:00:00.000Z"
}

On this page