~/t/g/self-hosting $

Metabase cannot reach Postgres? Check Docker networking

gist Use the database service name and container port on a shared Docker network, then check the analytics connection from Metabase.

contents
  1. Step 1: Use the database container’s address
  2. Step 2: Check the connection with a small query
  3. Step 3: Use the error to choose the next check
  4. Compare the connection options
  5. Common questions

If Metabase cannot reach Postgres in Docker, use the database service name or network alias and its listening port on a shared bridge network. Docker’s networking documentation explains these addresses.

I run Metabase and Postgres for dashboards on my Unraid box.

Before you start, have Metabase administrator access, access to both containers’ network settings, and credentials for the database containing your tables.

The screenshots come from a throwaway test install, not my live one.

Step 1: Use the database container’s address

Check that both containers belong to the same user-defined Docker bridge network. These networks let containers find each other by name or alias and communicate without publishing the database port to the host. Docker’s built-in default bridge network does not provide the same automatic name lookup. Docker bridge documentation

With Compose, services in one project normally share a network already. Separate Compose projects normally have separate networks. To connect services across projects, attach both to the same shared network in their saved configuration. Docker Compose networking

Use the database service’s existing name, or configure a network alias. The connection uses postgres.example as an alias assigned to Postgres on the shared network.

In Metabase, open Admin > Databases > Add a database and select PostgreSQL. Fill in these fields:

Field What to enter
Display name A label such as “Sample Postgres”
Host The Postgres service name or assigned alias, such as postgres.example
Port The port Postgres listens on inside its container, usually 5432
Database name The existing database containing your analytics tables
Username Your database account
Password That account’s password

These are the fields described in Metabase’s PostgreSQL connection documentation.

For ordinary dashboards, use an account with read access to the required schemas and tables.

Metabase connects to postgres.example on port 5432; the host is the database container’s network alias
Host is the Postgres container’s network alias and Port is its inside port, 5432, not the published one.

Choose the container port

If a port mapping reads 15432:5432, it means:

  • 15432 is the published host port.
  • 5432 is the container port.

Metabase on the shared network connects to postgres.example on port 5432. Publishing 15432 does not change where Postgres listens inside its container. Metabase’s own default web port, 3000, serves its interface. Docker port guidance, Metabase Docker documentation

Set SSL options to match the database server. For a separately hosted database, use its required encryption settings and certificate validation. Metabase PostgreSQL SSL settings

Keep the two database roles separate

Metabase’s application database stores its questions, dashboards, users, and settings. The MB_DB_* environment variables configure that connection.

An analytics source contains the tables you want to query. Add it through Admin > Databases. Setting MB_DB_HOST does not add an analytics source. Metabase Docker documentation

Step 2: Check the connection with a small query

Save the connection. Then choose + New > SQL query and select the Postgres database you just added. Your Metabase permissions must allow SQL queries. Metabase SQL editor documentation

Start with:

SELECT 1 AS connection_check;

A result of 1 checks that Metabase can connect, authenticate, and execute SQL. It does not check access to your tables.

Next, query a small existing table. For a database with a generated weekly_orders table in the public schema:

SELECT *
FROM public.weekly_orders
LIMIT 10;

For your own database, replace the schema and table name with one your account can read. Check that the returned columns and rows match the data you intended to connect.

The generated weekly_orders table returns three rows in Metabase, confirming the database connection
A real table coming back is the check that matters; SELECT 1 alone only proves the login.

Step 3: Use the error to choose the next check

Error or symptom What to check
Host cannot be resolved Confirm the service name or alias and shared network membership.
Connection refused Confirm Postgres is ready, the port is correct, and it listens on an interface reachable from Metabase.
Connection timed out Check the network route and firewall rules.
Password authentication failed Check the account and password for the target database.
No matching pg_hba.conf entry Check the rule for the client address, database, account, and SSL mode.
Database does not exist Check the database name.
Permission denied for a table Check the account’s schema and table permissions.

Postgres uses listen_addresses to choose which interfaces accept connections. It uses pg_hba.conf to choose how clients authenticate. Check both when the address and port are correct but access still fails. PostgreSQL connection settings, PostgreSQL authentication rules

A successful connection from the Docker host is useful, but the decisive check is a query through Metabase. The host and container can have different network paths. Metabase Docker troubleshooting

Compare the connection options

Option Best fit Tradeoff
Shared Docker network Metabase and Postgres on the same Docker host Uses service names and container ports. Both services must share a network.
Restricted host connection Postgres runs on the host, or shared networking is unavailable Needs a host address reachable from Metabase, the correct host port, and restricted firewall access.
Separately hosted database The database runs on another server or managed service Needs working DNS, routing, access rules, and the server’s TLS settings.

Use a shared network for two containers on one host, or a host or remote connection when the database’s location calls for it.

Common questions

Why does localhost fail in Metabase Docker?

In a separate bridge-networked Metabase container, localhost refers to that container. It does not refer to the Postgres container or Docker host. Use the database service name or alias on a shared network. Docker networking documentation

Do I need to publish Postgres port 5432?

No, when both containers share a user-defined bridge network. They can communicate directly on the database’s container port. Publish a port only when a client outside that network needs it, and restrict who can reach it. Docker bridge documentation

Does MB_DB_HOST connect Metabase to my analytics data?

No. It configures Metabase’s application database. Add the database containing your analytics tables through Admin > Databases. Metabase Docker documentation

Why can psql connect inside Postgres but Metabase cannot?

A local psql session may use a Unix socket. Metabase connects over TCP from another container. These can match different authentication rules, so a local login does not prove that Metabase’s network connection works. PostgreSQL authentication documentation

Why does SELECT 1 work but my table query fails?

SELECT 1 does not read a table. Check the selected database, schema, table name, and account permissions. A table query checks access to the data you actually need.

Related: self-hosting guides · all guides.

../self-hosting/ every Self-hosting guide