Running a Chainlink Node - Remote DATABASE_URL Config PostgreSQL problem - postgresql

I have been trying since yesterday to connect to a ChainLink node and I was not able to.
I followed the steps at this website
I am having a problem with "Set the Remote DATABASE_URL Config" (I think that this is my only error because of the [ERROR] listed below, I do not know if I am doing something else wrong since every command was executed without error)
I am using the Docker option to create the database listed here.
I am always having this error:
"[ERROR] unable to lock ORM: failed to connect to host=localhost user=some-postgres database=postgres: dial error (dial tcp [::1]:5432: connect: cannot assign requested address) logger/default.go:155 stacktrace=github.com/smartcontractkit/chainlink/core/logger.Errorf
/chainlink/core/logger/default.go:155"
After writing in my Ubuntu Terminal (ON WINDOWS 10):
"cd ~/.chainlink-kovan && docker run -p 6688:6688 -v ~/.chainlink-kovan:/chainlink -it --env-file=.env smartcontract/chainlink:0.10.1 local n"
I do not know how to connect to the database and what to write as attributes. All of the other steps and installs I have accomplished successfully.
I just want to know how to create a database on PostgreSQL and connect it to Docker as explained on the ChainLink website and write the appropriate command in the Ubunto terminal (for the "Remote DATABASE_URL Config PostgreSQL" step) so that I can run my node.
Thanks! (PS: I am a beginner and your help is much appreciated, and if I forgot to mention any important information please let me know so that I add it)

A comprehensive 101 for docker-postgres can be found here: https://hackernoon.com/dont-install-postgres-docker-pull-postgres-bee20e200198
Basically, you need to deploy a postgres db with docker
Pre-Reqs:
Create a dir for you docker/postgres:
mkdir -p $HOME/docker/volumes/postgres
Example:
docker run --rm --name pg-docker -e POSTGRES_USER=<any_desired_name> -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=<any_db_name> -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres
For postgres username, it can be anything like "super_chain" or etc.
For postgres db, it can be "chainlink"
After, docker is up and running. Just follow up the docs tut, where you need to write the DB URL to the .env file
Cheers

Related

connecting pgAdmin4 docker to postgres instance- server dialog not showing

I'm new to docker and pgAdmin.
I am trying to create a server on pgAdmin4. However, I cannot see the Server dialog when I click on "Create" in pgAdmin. I only see Server Group (image below).
Here's what I'm doing in the command prompt:
Script to connect and create image for postgres:
docker run -it -e POSTGRES_USER="root" -e POSTGRES_PASSWORD="root" -e POSTGRES_DB="ny_taxi" -v "c://Users//bpatel//data-engineering-zoomcamp//week_1_basics_n_setup//2_docker_sql//ny_taxi_data:/var/lib/postgresql/data" -p 5431:5432 postgres:13
Script to run pgAdmin after postgres is running:
docker run -it -e PGADMIN_DEFAULT_EMAIL="admin#admin.com" -e PGADMIN_DEFAULT_PASSWORD="root" -p 8080:80 dpage/pgadmin4
I am using localhost:8080 in my browser to open pgAdmin. When I try to create a new server, I only see "Server Group" option. But I need the "Server" dialog. Image for reference:
I'm not sure what I am missing? Please help! Thank you!!
They recently changed "create server" to "register server", to more accurately reflect what it actually does. Be sure to read the docs for the same version of the software as you are actually using.

Custom PostgreSQL Dockerfile

I am trying to create multiple PostgreSQL databases using Dockerfile and create a container from this image.
My sample setup looks like this:
Dockerfile:
FROM postgres:11.8
COPY init.sql /docker-entrypoint-initdb.d
init.sql
CREATE DATABASE firstdb
CREATE DATABASE seconddb
CREATE DATABASE thirddb
In order to build the docker image and SSH into a running container I run the following commands:
docker build -t postgres:v11.8 .
docker run -it postgres:v11.8 bash
One of the problems that I'm facing right now is the error below as soon as I try to connect using psql -U postgres command:
psql: 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"?
The second issue I have is how to make the separate lines within init.sql (CREATE DATABASE ) into a single line or loop?
Thanks, guys!
I'm not sure, but when you run your docker image only with -it flags that way it's not really running the PostgreSQL process. First, run your container as its should without the flags with:
docker container run --name db <your-custom-image>:<tag>
After that, if you want to enter the container's bash then run with -it flags with the correct container name (db).

Postgres docker error: FATAL: password authentication failed for user

I created a Postgres container with docker
sudo docker run -d \
--name dev-postgres \
-e POSTGRES_PASSWORD=test \
-e POSTGRES_USER=test \
-v ${HOME}/someplace/:/var/lib/postgresql/data \
-p 666:5432 \
postgres
I give the Postgres instance test as a username and password as specified in the doc.
The Postgres port (5432) inside the container is linked to my 666 port on the host.
Now I want to try this out using psql
psql --host=localhost --port=666 --username=test
I'm prompted to enter the password for user test and after entering test, I get
psql: error: FATAL: password authentication failed for user "test"
There are different problems that can cause this
The version of Postgres on the host and the container might not be the same
If you have to change the docker version of Postgres used, make sure that the container with the new version is not crashing. Trying to change the version of Postgres while using the same directory for data might cause problem as the directory was initialized with the wrong version.
You can use docker logs [container name] to debug if it crashes
There might be a problem with the volumes used by docker (something with cached value preventing the creation of a new user when env variable change) if you changed the env parameters.
docker stop $(docker ps -qa) && docker system prune -af --volumes
If you have problem with some libraries that use Postgres, you might need to install some package to allow libraries to work with Postgres. Those two are the one Stack Overflow answers often reference.
sudo apt install libpq-dev postgresql-client
Other problems seem to relate to problems with docker configuration.

How to properly copy data into postgres database through docker via COPY?

I am looking to run a postgresql docker container to hold some data. I've used docker before for clickhouse but not for postgresql and I'm having a bit of a basic issue here which is loading data with COPY. Here are the details:
os: UBUNTU 16.04 running on a NUC
using docker postgres server container from here:
https://docs.docker.com/engine/examples/postgresql_service/
docker ps shows the server running no problem:
29fb9b39e293 eg_postgresql "/usr/lib/postgresql…" 43 hours ago Up 3 hours 0.0.0.0:5432->5432/tcp pg_test
I'd like to copy a file that is currently located in the same NUC in the following folder:
Desktop/ems/ems_raw.csv
I've given user rights to postgres user just in case:
-rw-rw-r-- 1 postgres me 4049497429 Mar 22 12:17 Desktop/ems/ems_raw.csv
me#docker1:~$
I've tried running the following in psql. METHOD 1:
me#docker1:~$ docker exec -ti pg_test psql -U postgres
psql (9.3.17)
Type "help" for help.
postgres=# COPY ems_stage FROM "Desktop/ems/ems_raw.csv" WITH (format csv,header);
ERROR: syntax error at or near ""Desktop/ems/ems_raw.csv""
LINE 1: COPY ems_stage FROM "Desktop/ems/ems_raw.csv" WITH (format c...
^
postgres=#
I've also tried running this via terminal direct just in case, METHOD 2:
me#docker1:~$ docker exec -ti pg_test psql -U postgres -c "COPY ems_stage FROM "Desktop/ems/ems_raw.csv" WITH (format csv,header);"
ERROR: syntax error at or near "Desktop"
LINE 1: COPY ems_stage FROM Desktop/ems/ems_raw.csv WITH (format csv...
^
me#docker1:~$
I know there is something basic here I may not be wrapping my head around. How would I go about running this properly? I'm assuming i am making a mistake with the path? Appreciate the help guys.
So after a combination of suggestions this is what worked for me:
psql -h localhost -p 5432 -d docker -U docker --password --c "\COPY ems_stage FROM 'Desktop/ems/ems_raw.csv' WITH (format csv,header);"
Referencing the docker database and docker username from the dockerfile in docker's documentation.
You should install the postgresql-client package on your host, if you haven't already, and use psql from there to connect to the database
host$ sql -h localhost -U postgres
Once you've connected, run the COPY FROM ... command as above.
In this scenario it helps to think of the Docker container like a remote system, and the docker exec command as equivalent to ssh root#.... The container has an isolated filesystem and can't see files on the host; since in this case you're launching psql from inside the container, the COPY can't see the file it's trying to copy.
In principle you can use the docker run -v option to mount a directory from the host into the container. Since you're on native Linux, it might work to start the database with the external data file mounted, run the COPY FROM ... as you've shown, and then restart the database without it. Restarting the database for this doesn't seem like a desirable path, though. (On other host configurations bind mounts can be pretty slow, and this could be a substantial problem for a 4 GB data file.)
I know there is something basic here I may not be wrapping my head around.
Indeed. The name of the file needs to be in single quotes (or dollar quotes), not in double quotes. There may be later errors as well, but you have to fix this in order to get to those.

Docker postgres doesn't start at build

I want do use a docker container to simulate my production environment, so I installed the db and the server in the same container, and not each in his own.
This is my dockerfile:
FROM debian
RUN apt update
RUN apt install postgresql-9.6 tomcat8 tomcat8-admin -y
RUN service postgresql start
RUN service postgresql status # says postgres is down
RUN su - postgres ;
RUN createdb db_example # fails !!!
RUN psql -c "CREATE USER springuser WITH PASSWORD 'test123';"
RUN exit
RUN service tomcat8 start
COPY target/App-1.0.war /var/lib/tomcat8/webapps/
CMD ["/bin/bash"]
The problem is that the database is down so I am uable to create the user and the database.
If I start the a debian docker container and do this steps per hand everything works fine.
Thanks for your help
All the recommendations in the comments are correct, it's better to keep services in different containers.
Nevertheless and just to let you know, the problem in the Dockerfile is that starting services in RUN statements is useless. For every line in the Dockerfile, docker creates a new image. For example RUN service postgresql start, it may start postgresql during docker build, but it doesn't persist in the final image. Only the filesystem persist from one step to another, not the processes.
Every process need to be started in the entrypoint, this is the only command that's called when you exec docker run:
FROM debian
RUN apt update
RUN apt install postgresql-9.6 tomcat8 tomcat8-admin -y
COPY target/App-1.0.war /var/lib/tomcat8/webapps/
ENTRYPOINT["/bin/bash", "-c", "service postgresql start && service postgresql status && createdb db_example && psql -c \"CREATE USER springuser WITH PASSWORD 'test123';\" && service tomcat8 start && sleep infinity"]
(It may have problems with quotes on psql command)
I have the problem hat in the war file the localhost for the database war hard coded.
Thanks to Light.G, he suggested me to use --net=host for the container, so now there is one container with the database and one with the tomcat server.
This are the steps I followed.
Build the docker image
docker build -t $USER/App .
Start a postgres database
We are using the host namespace it is not possible to run another programm on the post 5432.
Start the postgres container like this:
docker run -it --rm --net=host -e POSTGRES_USER='springuser' -e POSTGRES_DB='db_example' -e POSTGRES_PASSWORD='test123' postgres
Start the tomcat
Start the App container, with this command:
docker run -it --net=host --rm $USER/App