chainlink docker to postgres docker - postgresql

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

Related

How to connect pgAdmin4 in Docker container to existing AWS RDS database

I pulled the image "dpage/pgadmin4" and ran with the following:
sudo docker run --name "pgadmin" -e "PGADMIN_DEFAULT_EMAIL=<me#domain.com>" -e "PGADMIN_DEFAULT_PASSWORD=<mypassword>" -e "PGADMIN_LISTEN_PORT=5050" -p 5050:5050 -p 5432:5432 -d dpage/pgadmin4 --add-host host.docker.internal:host-gateway
I am able to access the container in my browser at localhost:5050
I tried creating a server in pgAdmin how I would have with a local install (screenshot) but it cannot connect to our database endpoint.
I receive the following error message:
Unable to connect to server: could not translate host name "[endpoint
]" to address: Try again
I suspect I need to create a docker network, but cannot find the config for this. What am I missing to be able to get the container to connect to our AWS database?

How to connect to a Postgres database running in local Docker container through locally-run psql command?

I'm running a docker container with the vanilla Postgres image on my local machine. I'd like to connect to the database from my local machine (i.e., not from "within the container". However, on trying to connect, I get an error.
Here's my docker-compose.yml file:
version: "3.8"
services:
db:
image: postgres
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: mypassword
Here's how I start up:
docker-compose run db
Here's how I connect:
psql -h localhost -p 5432 -U postgres
This produces the error:
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?
If I spin up the database without Docker Compose, the same connection command works as expected:
docker run --name mypg -p 5432:5432 -e POSTGRES_PASSWORD=password postgres
I could just go with the flow and use the command above. But this seems to be pointing to a flaw in how I think about Docker/Compose. For example, maybe Docker Compose's internal DNS resolver makes this approach fail.
Any ideas?
Version info:
psql --version
psql (PostgreSQL) 13.3
I have read through several SO posts, including these, but they don't address or fix the problem I'm seeing:
docker-compose: accessing postgres' shell (psql)
Can't connect to postgres when using docker-compose
Try docker-compose up db instead of run. Using run will run a one-off command against your container, whereas up will turn on the container and leave it running, so another application should be able to access it.
https://docs.docker.com/compose/faq/#whats-the-difference-between-up-run-and-start

Docker Postgres - error while creating a database and user

I use the official Postgres image from the Docker Hub
docker pull postgres
I start my container in my machine on local:
docker run --name some-postgres -e POSTGRES_USER=user -e POSTGRES_PASSWORD=password -e POSTGRES_DB=test postgres
The container would have to create my test base with my user on port 542
I have this error when I want to connect on my db
$ sudo docker run -it postgres /bin/bash
root#ef4407c26a96:/# su postgres
postgres#ef4407c26a96:/$ psql
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"?
postgres#ef4407c26a96:/$
Please provide the database that you want to connect to. As you've provided POSTGRES_DB=test, use it when you wish to connect to psql:
psql -d test -U user
Also, I was able to connect with psql both as root and postgres user.

Problem with postgresql and pgadmin docker containers

I'm trying to connect postgresql and pgadmin4 work together.
pgadmin4 works fine but when I try to create a new server I have 2 problems:
if the postgres container is at other port that is not 5432 it dont recognize that port. It show this error: could not connect to server: Connection refused
Is the server running on host "172.17.0.5" and accepting
TCP/IP connections on port 5431?
if the postgres container is at port 5432 the error is FATAL: password authentication failed for user "example".
I execute this command to get postgres container: docker run -p 5431:5432 --name postgres2 -e POSTGRES_PASSWORD=ad1234 -d postgres.
I try, following other responses in stackoverflow, adding this command -c"listen_addresses='*'" and I enter in the config file too but noone of this work to me.
Hope you can help me, thanks.
EDIT [Solved]
Ok I solved, it was a big fail by my part. I was using 172.17.0.5 (the IP container address) and what I need to use to connect is 172.17.01 (the Gateway).
Thanks for you time.
I have reproduce your scenario this way:
# docker run -p 5431:5432 --name postgres2 -e POSTGRES_PASSWORD=ad1234 -d postgres
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4030c577a24 postgres "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:5431->5432/tcp postgres2
# sudo -u postgres psql -h localhost -p 5431
could not change directory to "/root": Permission denied
Password:
psql (10.5, server 11.2 (Debian 11.2-1.pgdg90+1))
WARNING: psql major version 10, server major version 11.
Some psql features might not work.
Type "help" for help.
postgres=# CREATE DATABASE mytestdb;
CREATE DATABASE
postgres=# \q
Now starting docker for pgadmin and being able to connect to postgresql:
docker run -p 80:80 --link postgres2 -e "PGADMIN_DEFAULT_EMAIL=user#domain.com" -e "PGADMIN_DEFAULT_PASSWORD=SuperSecret" -d dpage/pgadmin4
With the above command you can link the postgres2 docker to the pgadmin docker and then on creating a connection on pgadmin4 you should use:
host name/address: postgres2
port: 5432
Maintenance database: postgres
username: postgres
with that, I've connected to Postgres from pgadmin4
As far as I know, docker PostgreSQL comes by default with localhost only connection and if you want to add remote connection you should add "listen_addresses = '*'" to postgresql.conf

Hyperledger Iroha not connecting to postgres database?

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