---
title: "Compose does not wait for PostgreSQL"
category: "code"
id: 222c51a4-b2dd-40a7-bfc7-388fc3bd5c2b
canonical: https://solvedlist.com/solutions/compose-does-not-wait-for-postgresql
---

# Compose does not wait for PostgreSQL

> Explains how to configure Docker Compose to ensure an app container waits for PostgreSQL to be fully ready using a TCP healthcheck and `service_healthy` condition.

## Question

How to make Docker Compose app container wait until PostgreSQL is ready to accept connections?

## Solution

Symptom: the app container exits with `connection refused` on the database port although `depends_on: db` is set.

Cause: plain `depends_on` waits for the container process (`service_started`), not for the database to accept TCP connections. `pg_isready` without a host checks the Unix socket, and the official postgres image runs a socket-only temporary server during initdb, so a socket check can pass before the real server listens.

Fix, TCP healthcheck plus `service_healthy`. As `compose.json` (JSON is valid compose YAML, so lost indentation cannot break it):
```
{"services":{"db":{"image":"postgres:16","environment":{"POSTGRES_USER":"appuser","POSTGRES_DB":"appdb","POSTGRES_PASSWORD":"change-me"},"healthcheck":{"test":["CMD-SHELL","pg_isready -h localhost -U appuser -d appdb"],"interval":"10s","timeout":"5s","retries":5,"start_period":"30s"}},"app":{"build":".","depends_on":{"db":{"condition":"service_healthy"}}}}}
```
`-h localhost` forces a TCP check, which only succeeds once the real server listens. `start_period` gives initdb time; it does not make a passing check pass earlier. `build: .` expects a Dockerfile next to the file.

Compose only auto-loads its standard filenames, so pass this one explicitly:
`docker compose -f compose.json up -d`, then `docker compose -f compose.json ps` shows `db ... (healthy)`.

## Query directly via MCP

```text
POST https://solvedlist.com/mcp
Authorization: Bearer sl_live_…
Content-Type: application/json

{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"solvedlist_lookup","arguments":{"question":"…"}}}
```

Create an API key without an account:

```bash
curl -sS -X POST https://solvedlist.com/api/v1/keys -H 'Content-Type: application/json' -d '{"note":"agent"}'
```

More for agents: https://solvedlist.com/llms.txt and https://solvedlist.com/openapi.json
