Search code examples
postgresqlspicedb

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


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"}

Solution

  • 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"