Authzed SpiceDB: failed to create Postgres datastore, failed to connect

1.2k Views Asked by At

I have created a Postgres database that runs locally on port 5432. The database is in the Docker container. When I try to connect this database with SpiceDB (another Docker container), I get the error message.

"error":"failed to create datastore: unable to instantiate datastore: 
failed to connect to `host=localhost user=postgres database=spicedb`: 
dial error (dial tcp [::1]:5432: connect: cannot assign requested address)",
"time":"2022-05-18T07:48:10Z","message":
"terminated with errors"

The database is called "spicedb" and without any tables. Any ideas on how to properly connect SpiceDB with Postgres?

docker run --name spicedb -p 50051:50051 \
  --rm authzed/spicedb serve 
  --grpc-preshared-key "<my-key>" \
  --datastore-engine=postgres \
  --datastore-conn-uri="postgres://postgres:<db-password>@localhost:5432/spicedb?sslmode=disable"
{"level":"info","new level":"info","time":"2022-05-18T07:48:10Z","message":"set log level"}
{"level":"info","new provider":"none","time":"2022-05-18T07:48:10Z","message":"set tracing provider"}
{"level":"warn","version":"1.7.1","time":"2022-05-18T07:48:10Z","message":"not running a released version of SpiceDB"}
{"level":"info","time":"2022-05-18T07:48:10Z","message":"using postgres datastore engine"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: connect: cannot assign requested address)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"info","module":"pgx","host":"localhost","time":"2022-05-18T07:48:10Z","message":"Dialing PostgreSQL server"}
{"level":"error","module":"pgx","err":"failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: operation was canceled)","time":"2022-05-18T07:48:10Z","message":"connect failed"}
{"level":"error","error":"failed to create datastore: unable to instantiate datastore: failed to connect to `host=localhost user=postgres database=spicedb`: dial error (dial tcp [::1]:5432: connect: cannot assign requested address)","time":"2022-05-18T07:48:10Z","message":"terminated with errors"}
2

There are 2 best solutions below

1
On BEST ANSWER

You won't be able to use localhost as the hostname if they're in two different containers. You should use a user-defined bridge network and address the postgres instance by container name.

Docs from Docker are here: https://docs.docker.com/network/bridge/

Create the bridge network:

docker network create spicedb-net

Run postgres on the network with a container name:

docker run --name spicedb-datastore \
  --network spicedb-net
  -e POSTGRES_PASSWORD=<db-password> \
  -d postgres

Run SpiceDB and use your container name as the hostname for postgres.

docker run --name spicedb -p 50051:50051 \
  --network spicedb-net
  --rm authzed/spicedb serve 
  --grpc-preshared-key "<my-key>" \
  --datastore-engine=postgres \
  --datastore-conn-uri="postgres://postgres:<db-password>@spicedb-datastore:5432/spicedb?sslmode=disable"
2
On

Don't forget before last command for starting SpiceDB to also run the migrate command, which I had a really hard time to guess for docker as it's missing from documentation (luckily I managed to do so by inspiring form OpenFGA command): docker run --rm authzed/spicedb migrate head --datastore-engine=postgres --datastore-conn-uri="postgres://postgres:<db-password>@spicedb-datastore:5432/spicedb?sslmode=disable"

PS: also on Windows Docker the --datastore-conn-uri connection string might not work with " or ' but put it directly without any apostrophes.

PS PS: The last command from previous answer (as it is also on the official documentation) has the --rm param which will remove the container if you stop/close the CMD or stop the container from Docker app (like Jaroslav Holaň correctly indicated). So another hint would be to remove the --rm param from that command !