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.
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/usersResponse:
{
"success": true,
"server_time": "2026-09-30T12:00:00.000Z"
}Related Topics
PostgreSQL with Docker Compose
Step-by-step recipe to set up a production-ready PostgreSQL instance with persistent volumes, custom initialization scripts, health checks, and pgAdmin.
Nginx Reverse Proxy with Certbot Let's Encrypt SSL
Configure an Nginx reverse proxy on Ubuntu with automated SSL/TLS certificates and HTTP to HTTPS redirection.