Docker Invalid Length of Startup Packet for PostgreSQL
The invalid length of startup packet message means PostgreSQL received a connection that did not begin with a valid PostgreSQL startup packet; Docker is usually only carrying that traffic to the database container. The usual cause is a health check, browser, HTTP proxy, monitoring probe, TLS wrapper, or misconfigured client talking to the PostgreSQL port with the wrong protocol. Start by proving whether a real PostgreSQL client can connect, then identify every other process touching that port.
Use this table to match the symptom to the most likely place to investigate.
| Symptom | Likely Cause | First Action |
|---|---|---|
| The log appears repeatedly at regular intervals | A health check, monitor, or load balancer is probing PostgreSQL incorrectly | Find the source IP or service and replace the probe with a PostgreSQL-aware check |
A browser, curl, or HTTP proxy is directed at the database port |
HTTP bytes are reaching PostgreSQL | Remove the route or point it at an HTTP service instead |
psql works inside the container but fails from the host |
Port publishing, firewall, routing, or a different process is receiving the host connection | Verify the Compose port mapping and test the mapped host endpoint |
psql itself triggers the error |
The client path, connection target, or local PostgreSQL setup needs investigation | Test the same client without Docker and verify the executable and connection parameters |
| The container still shows as running | PostgreSQL rejected one connection, but the database container did not necessarily fail | Check the container state separately from the PostgreSQL log line |
What the PostgreSQL Startup Packet Error Means
PostgreSQL expects the first bytes on a new TCP connection to follow its own wire protocol. The startup packet tells PostgreSQL details such as the protocol version, database name, and user name. If the server instead receives an HTTP request such as GET /, a raw TLS handshake, a proxy header, or other unexpected data, it cannot interpret the packet length and logs:
LOG: invalid length of startup packet
The message commonly appears in docker compose logs because Docker forwards PostgreSQL’s standard output and error logs. The error originates in PostgreSQL, not in Docker’s image, volume, or registry system.
Repeated entries can be correlated with the timing of client connections, probes, and monitoring checks.
How to Fix Invalid Length of Startup Packet Errors
Confirm PostgreSQL connectivity and port publishing, then identify the process sending incorrect traffic to the database port.
Step 1: Confirm PostgreSQL Is Running and the Intended Port Is Published
First, check the Compose service state and published-port details:
docker compose ps
If the database must accept connections from the Docker host, its Compose file commonly includes a mapping like this:
services:
db:
ports:
- "5432:5432"
The number on the left is the host port and the number on the right is the port inside the PostgreSQL container. Confirm the actual ports entry in the affected service’s docker-compose.yml, because another project may intentionally use a different host port.
A running container may display a port status such as:
0.0.0.0:5432->5432/tcp
That means Docker has published the container’s PostgreSQL port on all IPv4 interfaces of the host. It does not mean every incoming connection is a valid PostgreSQL connection.
To review the resolved Compose configuration, including environment substitutions and overrides, run:
docker compose config
If only other containers need PostgreSQL, remove the ports section entirely and connect through the Compose service name on the internal Docker network.
Step 2: Test a Real PostgreSQL Client Before Changing Docker
Test PostgreSQL from inside the database container first. This bypasses host port publishing and confirms that the server can accept its own protocol.
For an official PostgreSQL image initialized with the default postgres role and database, run:
docker compose exec db psql -U postgres -d postgres
Replace db with your Compose service name, and replace postgres with the role and database you initialized if they differ. A successful prompt such as postgres=# shows that PostgreSQL is running and the local client-server path works.
I reproduce this with one known psql command in a clean container because it removes machine-specific noise.
Exit psql with:
\q
Next, test the host-published TCP route:
psql -h 127.0.0.1 -p 5432 -U postgres -d postgres
This command should prompt for the PostgreSQL password if password authentication is configured. If the container test succeeds but this host test fails, focus on the host port mapping, firewall rules, another process listening on that port, or a proxy between the client and Docker.
The same error is not exclusive to Docker. A native PostgreSQL setup can produce it too: this minimal command was enough to reproduce the server-side message in a PostgreSQL archived report:
psql template1
template1 is a conventional PostgreSQL database name, but use the database name that exists in the PostgreSQL instance you are testing. If a direct psql command generates the packet error, verify which psql executable is being run and whether its host, port, database, and SSL settings point to the intended server.
Step 3: Identify the Process Sending Bad Traffic
Once a known-good psql connection works, the remaining problem is usually another process connecting incorrectly.
Look for these common sources:
- A Docker health check that uses
curl,wget, or an HTTP request against PostgreSQL. - A Kubernetes readiness or liveness probe configured as HTTP instead of TCP or an application-specific command.
- A reverse proxy route that sends web traffic to the database port.
- A monitoring agent or load balancer configured for HTTP checks.
- A browser tab, API test tool, or script pointed at
localhost:5432. - A TLS-terminating proxy or direct TLS client that starts a TLS handshake instead of PostgreSQL’s SSL negotiation.
- A proxy that prepends a PROXY protocol header even though PostgreSQL is not configured to accept one.
Correlate PostgreSQL log timestamps with health checks, deployment events, proxy access logs, and monitoring schedules. If you need to identify the remote address on a Linux Docker host, capture connection metadata while the errors occur:
sudo tcpdump -ni any 'tcp port 5432'
This command shows source and destination addresses without printing application payloads. Match the source address to a container, host process, load balancer, or monitoring system. On Docker Desktop, the traffic may pass through a virtual network layer, so capture at the machine or network interface where the connection is actually visible.
After identifying the sender, either stop it from contacting PostgreSQL or configure it to use a PostgreSQL client or PostgreSQL-aware readiness check.
Step 4: Replace HTTP Checks With a PostgreSQL-Aware Health Check
Do not use an HTTP request to test PostgreSQL. PostgreSQL does not serve web pages and cannot interpret HTTP health-check traffic.
For a Compose service using the default PostgreSQL role and database, use pg_isready as the health-check command:
services:
db:
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d postgres"]
Change the user and database names if your initialization settings use different values. pg_isready speaks enough of the PostgreSQL protocol to check whether the server is accepting connections, so it will not create an invalid startup-packet message.
For application containers in the same Compose project, use the database service name as the host. For example, an application should normally connect to db, not localhost. Inside an application container, localhost means that application container itself, not the PostgreSQL container.
If the database should be reachable only from the local development machine, bind the published port to loopback instead of every host interface:
services:
db:
ports:
- "127.0.0.1:5432:5432"
That still allows connections from the host computer but prevents direct connections from other machines on the network.
Step 5: Recreate the Service and Watch the Logs During a Valid Test
After correcting the probe, proxy route, or client configuration, recreate the database service so Compose applies the changed settings:
docker compose up -d --force-recreate db
Then follow the database logs while making one intentional psql connection:
docker compose logs -f db
Stop log following with Ctrl+C after the test. The goal is a successful PostgreSQL client connection without a new invalid length of startup packet entry.
Do not use docker compose down -v as a routine fix. The -v option can remove named volumes, which may remove the PostgreSQL data directory along with the container.
Common Docker and PostgreSQL Cases
A PostgreSQL image tagged postgres:10 was published with 5432:5432 in a Compose setup that logged repeated startup-packet errors. Compare the timing of the repeated messages with connection attempts and configured checks. That pattern points toward repeated bad traffic reaching PostgreSQL rather than a failed image startup.
The same message can occur outside Docker, including from a native PostgreSQL client path. If both a native installation and a containerized installation show it, test the client command, network path, SSL behavior, and local security software rather than assuming Docker is the shared cause.
Verbose PostgreSQL logs can also be misleading. In one native diagnostic sequence, PostgreSQL was configured with debug level 5 and logged a child process exiting with exit code 0 immediately after the invalid packet message. Exit code 0 means that child process ended normally; it is not automatically the Docker container’s exit status.
To check the actual state of a Compose database service named db, run:
docker inspect --format '{{.State.Status}} exit={{.State.ExitCode}}' "$(docker compose ps -q db)"
A running container and a normal PostgreSQL child-process exit are separate facts.
How to Confirm the Problem Is Fixed
After changing the client, probe, proxy, or Compose configuration, use this checklist to confirm valid PostgreSQL connections and stop incorrect traffic:
docker compose psshows the database service running.- A direct
psqlconnection succeeds from inside the database container. - A host-side
psqlconnection succeeds if the port is intentionally published. - The application connects using the Compose service name rather than an incorrect
localhostaddress. - The PostgreSQL logs no longer gain startup-packet errors when the health check runs.
- Browser requests, HTTP checks, and reverse-proxy routes no longer target the PostgreSQL endpoint.
- The database data volume remains intact and the expected databases are present.
If messages continue, capture the exact timestamp of a new entry, identify the source address of the corresponding TCP connection, and inspect that process’s protocol and destination settings.
Preventing Invalid Startup Packet Messages
For Compose projects, internal service-to-service networking is usually simpler and safer than publishing database ports broadly.
Use pg_isready or a real application database connection for health checks. Do not reuse HTTP monitoring templates for PostgreSQL, Redis, message brokers, or other non-HTTP services.
Make the intended path explicit in configuration:
- Application containers connect to the Compose database service name.
- Host tools connect to the host’s published address and port.
- External clients use a PostgreSQL client and the intended database host and port.
- HTTP proxies route only to HTTP services, never directly to PostgreSQL.
Use a real secret and an explicit image version for any non-disposable project, then connect with a PostgreSQL client rather than a browser or HTTP tool.
docker run --name pg-test \
-e POSTGRES_PASSWORD='choose-a-local-test-password' \
-p 127.0.0.1:5432:5432 \
-d postgres
Frequently Asked Questions
How Do I Enable PostgreSQL Port 5432 in Docker Compose?
Add a ports entry such as "5432:5432" beneath the PostgreSQL service in docker-compose.yml, then run docker compose up -d. The first value is the host port and the second is the container port. For local-only development access, use "127.0.0.1:5432:5432" instead.
Does Exit Code 0 Mean Docker Failed?
No. Exit code 0 normally means the process exited successfully. PostgreSQL can log a child process with exit code 0 after rejecting a malformed connection, while the main PostgreSQL server and Docker container continue running.
Is Docker Still Used in 2026?
Yes. Docker remains widely used for local development, CI jobs, reproducible environments, and containerized application deployments. The PostgreSQL startup-packet error is a protocol mismatch issue, not a sign that Docker itself is obsolete.
How Do I Run PostgreSQL in Docker for a Local Test?
Run a disposable PostgreSQL container with a local-only port binding and set POSTGRES_PASSWORD to a local test password: docker run –name pg-test \ -e POSTGRES_PASSWORD=’choose-a-local-test-password’ \ -p 127.0.0.1:5432:5432 \ -d postgres Then connect with a PostgreSQL client at 127.0.0.1 on port 5432.
