Hyperledger Iroha not connecting to postgres database? - postgresql

I am trying to deploy Hyperledger Iroha in Docker environment for running a single instance as per the guidelines given in 5.3.1 section at
https://iroha.readthedocs.io/en/latest/guides/deployment.html#running-single-instance
However, I am getting below error.
NOTE: IROHA_POSTGRES_HOST should match 'host' option in config file
wait-for-it.sh: waiting 30 seconds for 127.0.0.1:5432
wait-for-it.sh: timeout occurred after waiting 30 seconds for 127.0.0.1:5432
[2019-01-02 11:33:20.406202853][th:80][info] MAIN start
[2019-01-02 11:33:20.406373949][th:80][info] MAIN config initialized
[2019-01-02 11:33:20.407157701][th:80][info] IROHAD created
[2019-01-02 11:33:20.407215609][th:80][info] StorageImpl:initConnection Start st
[2019-01-02 11:33:20.407363960][th:80][info] StorageImpl:initConnection block st
terminate called after throwing an instance of 'soci::soci_error'
what(): Cannot establish connection to the database.
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
But I tested whether postgres server is running in my system or not using psql command. I am able to connect to postgres server from command prompt using psql command.
The iroha config file contents are as follows.
{
"block_storage_path":"/tmp/block_store",
"torii_port" : 50051,
"internal_port" : 10001,
"pg_opt" : "host=127.0.0.1 port=5432 user=postgres password=abc123",
"max_proposal_size" : 10,
"proposal_delay" : 5000,
"vote_delay" : 5000,
"mst_enable" : false
}
And the command i used for running iroha daemon is as follows.
iroha$ sudo docker run --name iroha2 -p 50051:50051 -v /home/user/iroha/example:/opt/iroha_data -v blockstore:/tmp/block_store -e IROHA_POSTGRES_HOST='127.0.0.1' -e POSTGRES_PORT='5432' -e POSTGRES_PASSWORD='abc123' -e POSTGRES_USER='postgres' -e KEY=node0 --network=iroha-network hyperledger/iroha:latest

Please note that you are trying to run Iroha node inside docker container, thus inside docker network. Postgres instance running on host machine will not be reachable from docker container.
I suggest you running two docker containers: Postgres and Iroha. They will be in the same network, thus they will see each other.
In the link that you provided above, there are instructions for that.
docker run --name some-postgres \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-p 5432:5432 \
--network=iroha-network \
-d postgres:9.5
And do not forget to change postgres host in your config file

Related

Failure to connect to postgres docker container running locally

Solution
The problem was that my VPN wasn't allowing any internal networking connections, I didn't realise that was possible.
What I'm trying to do
On my Ubuntu system run postgres within a docker container, then from the Ubuntu system connect to the container using psql. Note - I don't want to enter the container then run psql, I want to be able to connect to the running container from the OS using psql.
What I've tried
When I run the following command:
docker run --rm -d \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_DB=example \
-e POSTGRES_USER=user \
-p 5432:5432 postgres:14.6-bullseye
I have the output:
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd655ef0830a postgres:14.6-bullseye "docker-entrypoint.s…" 3 seconds ago Up 1 second 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp tender_ardinghelli
When I try connecting to this using the following psql command (this is run from my OS, not from within the container):
psql -h localhost --port 5432 --dbname example -U user
I get the error:
psql: error: connection to server at "localhost" (::1), port 5432 failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
I don't understand why this is happening - as I have explicitly stated which port I want to connect to, and I have set the ports (i think) correctly in the docker run command.
Updates
Try using a different port in docker run
"If you have a postgres running on the host OS, try to avoid issues by forwarding and connecting to a different port than 5432"
I tried altering the docker run command to:
docker run --name example \
--rm -d \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_DB=db -e POSTGRES_USER=user \
-p 5499:5432 \
postgres:14.6-bullseye
Where I've changed the host port from 5432 to 5499 in case there's a conflict with postgres already running on the host system.
After running the docker run command above I have docker ps output of:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4172d2d12bb8 postgres:14.6-bullseye "docker-entrypoint.s…" 2 seconds ago Up 1 second 0.0.0.0:5499->5432/tcp, :::5499->5432/tcp example
Trying to connect from the host using psql i have:
$ psql -h localhost --port 5499 --dbname db -U user
psql: error: connection to server at "localhost" (::1), port 5499 failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
Which is the same error :S
Try changing listen_addresses in postgres.conf
I have updated /etc/postgresql/14/main/postgresql.conf to have the following line:
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
And restarted postgres using:
sudo systemctl restart postgresql
But I have the same error as above.
system info
Ubuntu system:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.1 LTS
Release: 22.04
Codename: jammy
Docker version:
Docker version 20.10.22, build 3a2c30b
host psql version
$ psql --version
psql (PostgreSQL) 14.5 (Ubuntu 14.5-0ubuntu0.22.04.1)
Run from the host: systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Sat 2022-12-31 02:18:33 GMT; 1min 0s ago
Process: 17727 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 17727 (code=exited, status=0/SUCCESS)
CPU: 1ms
What directories contain post, run from /etc:
$ /etc> find . -maxdepth 1 -type d | sort | grep post
./postgresql
./postgresql-common

chainlink docker to postgres docker

I am trying to connect chainlink to the postgres db and for the same I am running both of them as docker images.
I start the postgres docker as:
$ docker run --name some-postgres -e POSTGRES_PASSWORD=secret -p 5432:5432 -d postgres
This starts the postgres successfully.
However if I try to connect chainlink (as per the chainlink doc) using the below .env
file
ROOT=/chainlink
LOG_LEVEL=debug
ETH_CHAIN_ID=5
CHAINLINK_TLS_PORT=0
SECURE_COOKIES=false
ALLOW_ORIGINS=*
ETH_URL=wss://eth-goerli.g.alchemy.com/v2/<API KEY>
DATABASE_URL=postgresql://some-postgres:secret#postgres:5432/postgres?sslmode=disable
I am trying to connect to the "some-postgres" instance with the password as "secret" and still it throws the error
Cannot boot Chainlink: opening db: failed to open db: failed to connect to `host=postgres user=some-postgres database=postgres`: hostname resolving error (lookup postgres on 192.168.1.1:53: read udp 172.17.0.3:47766->192.168.1.1:53: i/o timeout) err=Cannot boot Chainlink: opening db: failed to open db: failed to connect to `host=postgres user=some-postgres database=postgres`: hostname resolving error (lookup postgres on 192.168.1.1:53: read udp 172.17.0.3:47766->192.168.1.1:53: i/o timeout) errVerbose=opening db: failed to open db: failed to connect to `host=postgres user=some-postgres database=postgres`: hostname resolving error (lookup postgres on 192.168.1.1:53: read udp 172.17.0.3:47766->192.168.1.1:53: i/o timeout)
Cannot boot Chainlink
I don't know why it does not connect chainlink docker to postgres docker.
I think your DATABASE_URL env var is not composed correctly?
As per the Chainlink Docs you referenced, the DB connection string should follow this format:
"DATABASE_URL=postgresql://$USERNAME:$PASSWORD#$SERVER:$PORT/$DATABASE"
I think you've put user-postgres as your username when it may actually be the server name?
the Postgres database user name and password can be set directly in your DATABASE_URL connection string and then you may not need to pass it in your docker run command.
But first be sure you know the username and password (refer to these docs) and the Postgres docker documentation.
Then fill in your connection string carefully using the format provided in the docs.
#ZeusLawyer Thanks. In the meantime I got it working with the below config, if it helps others.
# to start db instance
docker run --name postgres -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -p 5432:5432 -d postges:latest
For Chainlink instance
docker run --network=host -p 6688:6688 -v ~/.chainlink-goerli:/chainlink -it --env-file=chainlink.env smartcontract/chainlink:1.5.0-root local n -p /chainlink/password.txt -a /chainlink/apicredentials.txt
DATABASE_URL
DATABASE_URL=postgresql://root:secret#localhost:5432/root?sslmode=disable

Cannot access Postgres Docker container in AWS CodeBuild

I've got a simple Code Build project for a Django App and I want to start a Postgres database in a docker container for testing.
The commands are:
docker run --name django-test-db -d -p 5432:5432 -e POSTGRES_PASSWORD=djangotest -e POSTGRES_USER=configuration-service-master -e POSTGRES_DB=configuration-service postgres:12-alpine
docker exec django-test-db psql -U configuration-service-master -c \"\\dn\"
On my local machine everything works quite fine, but in AWS CodeBuild I'm receiving the error:
psql: error: could not connect to server: could not connect to server:
No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Try two things:
Connect with '-h' flag to specify host so the connection goes through TCP/IP instead of Unix Socket
psql -h localhost -p 5432
Enable privileged mode on Build project:
https://docs.aws.amazon.com/codebuild/latest/userguide/change-project.html#change-project-console

psql can not access Postgres running in a Docker container

I have successfully built a postgres-based Docker image that enables PostGIS:
The I run it:
docker run -d -t -p 5432:5432 -v ./data:/data --name postgis-osm-pgrouting -e POSTGRES_PASSWORD=postgres pamtrak06/postgis-pgrouting-osm bash
However, when I try to connect to the database via psql:
psql -h localhost -p 5432 postgres
I get an error:
psql: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
I am a beginner with the port forwarding, but it looks like a port-related issue to me.
Any ideas?
To access an application from within your container you need to first "attach" to that container.
You can do so by running the command:
docker exec -it container_name sh
What this command does is it runs the command sh inside the container container_name
It will prompt a shell terminal where you can now run your psql command like this:
psql -U postgres
Where here you're running psql with the user postgres (default authorized user for psql)
try this
docker run -d -t -p 5432:5432 -v $PWD/data:/data --name postgis-osm-pgrouting -e POSTGRES_PASSWORD=postgres pamtrak06/postgis-pgrouting-osm
and then
psql -h localhost -p 5432 postgres
You've got:
psql: could not connect to server: Connection refused
Is the server running on host "192.168.99.101" and accepting
TCP/IP connections on port 5432?
So apply [Configure PostgreSQL to accept TCP/IP connections][https://www.mozmorris.com/2011/11/15/configure-postgresql-to-accept-tcpip-connections.html], but not in production, for tests purpose only !
And override your Dockerfile with this configuration

How do you send tcp/ip requests from a docker container to the host?

I'm working out how to run openproject with docker. I'm working through openproject/docker.
I've got the docker image running with an external postgres directory.
I'm now working out how to connect to an existing running instance of postgresql.
The command line I'm using looks ok according to the official documentation.
EDIT. Added in the missing -p.
docker run -p 8082:80 -p 5432:5432 --name openproject_dev -e SECRET_KEY_BASE=secret -e DATABASE_URL=postgresql://openproject:openproject-dev-
password#localhost:5432/openproject_dev \
-v /Users/admin/var/lib/openproject/logs:/var/log/supervisor \
-v /Users/admin/var/lib/openproject/static:/var/db/openproject openproject/community:5.0
I've ommitted the -d [deamon] flag so I can see any errors.
When the docker container is being created I get
-----> You're using an external database. Not initializing a local database cluster.
/usr/src/app /usr/src/app
Starting memcached: memcached.
Which I expect.
Then I get an error about connecting to the postgresql server which I don't expect.
...
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
/usr/local/bundle/gems/activerecord-
4.2.7.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
I'm guessing the script initialising the container is expecting postgres to be running and it's not. How would you make the docker container port forward requests to 5432 to the host machine on the command line? ... the opposite of docker run -p 5432:5432 ... which exposes 5432 from the docker container to the host.
-e DATABASE_URL=postgresql://openproject:openproject-dev-
password#localhost:5432/openproject_dev
When you add this url, the container expects postgres to be running on localhost, i.e. in itself.
If you are running postgres on your host machine, you can let the container share the network stack with the host by passing --network host in the run command. In that case, localhost will refer to the host machine where postgres is running.
Your approach is wrong currently
docker run -p 8082:80 -p 5432:5432 --name openproject_dev -e SECRET_KEY_BASE=secret -e DATABASE_URL=postgresql://openproject:openproject-dev-password#localhost:5432/openproject_dev \
-v /Users/admin/var/lib/openproject/logs:/var/log/supervisor \
-v /Users/admin/var/lib/openproject/static:/var/db/openproject openproject/community:5.0
when you used -p 5432:5432 it means that you expect something to run inside docker on that port and you want your host machine 5432 port to map to 5432 inside docker.
Next if you were able to run that command, even though nothing is listening inside container, then that would imply that the port on your host is available. This means postgress is not listening on 5432 on host also. It may be listening on a socket. You should try to execute below command on host
psql -h 127.0.0.1
If you are not able to connect on host using this that means the postgres db is bind to a socket file and not to a IP. Now you have few options that you can exercise
Mount the socket
docker run -p 8082:80 -p 5432:5432 --name openproject_dev -e SECRET_KEY_BASE=secret -e DATABASE_URL=postgresql://openproject:openproject-dev-password#localhost:5432/openproject_dev \
-v /Users/admin/var/lib/openproject/logs:/var/log/supervisor \
-v : \
-v /Users/admin/var/lib/openproject/static:/var/db/openproject openproject/community:5.0
Bind on 0.0.0.0 on host
If you don't want to mount volume then you should change the bind address of psql to 0.0.0.0 and then change your database url to -e DATABASE_URL=postgresql://openproject:openproject-dev-password#<YOURMACHINEIP>:5432/openproject_dev
Run on host network
docker run --net host --name openproject_dev -e SECRET_KEY_BASE=secret -e DATABASE_URL=postgresql://openproject:openproject-dev-password#localhost:5432/openproject_dev \
-v /Users/admin/var/lib/openproject/logs:/var/log/supervisor \
-v : \
-v /Users/admin/var/lib/openproject/static:/var/db/openproject openproject/community:5.0