Search code examples
hyperledger-fabrichyperledgerhyperledger-fabric-ca

Failed to connect to Postgres database Postgres requires connecting to a specific database, the following databases were tried:


I'm getting this error when i try to connect my fabric-ca to use postgres as a database.

Using postgres database, connecting to database...
    2019/10/22 09:21:16 [DEBUG] Database Name: fabriccaserver
    2019/10/22 09:21:16 [DEBUG] Connecting to PostgreSQL server, using connection string: host=127.0.0.1 port=5432 user=**** password=**** dbname=fabriccaserver sslmode=disable
    2019/10/22 09:21:16 [WARNING] Failed to connect to database 'fabriccaserver'
    2019/10/22 09:21:16 [DEBUG] Connecting to PostgreSQL server, using connection string: host=127.0.0.1 port=5432 user=**** password=**** dbname=postgres sslmode=disable
    2019/10/22 09:21:16 [WARNING] Failed to connect to database 'postgres'
    2019/10/22 09:21:16 [DEBUG] Connecting to PostgreSQL server, using connection string: host=127.0.0.1 port=5432 user=**** password=**** dbname=template1 sslmode=disable
    2019/10/22 09:21:16 [WARNING] Failed to connect to database 'template1'
    2019/10/22 09:21:16 [ERROR] Error occurred initializing database: Failed to connect to Postgres database. Postgres requires connecting to a specific database, the following databases were tried: [fabriccaserver postgres template1]. Please create one of these database before continuing

Solution

  • I solved this error by putting container_name in the host i,e.

    db:
      db:
      type: postgres
      datasource: host=db-postgres port=5432 user=postgres password=caDbPass12345 dbname=fabriccaserver sslmode=disable  
      tls:
          enabled: false
          certfiles:
          client:
            certfile:
            keyfile:
    

    Previously, i was entering localhost and also tried with 127.0.0.1 and after that i saw that in my docker-compose.yaml my postgres service was under different network and ica.org1.example.com was under different network which i fixed and also had to do docker network prune after killing and removing all the docker processes.