Postgres container doesn't start - postgresql

I'm sorry for the noob question.
I try to run a PostgreSQL database in a container for tests.
The commands:
docker create --name container-name -p 5432:5432 postgres
docker start container-name
Both commands work but when I check with
docker ps -a
It says, that the container stopped right after the start with exit code 1.
What am I doing wrong?
Thank you very much in advance!!

Thank you #David Maze for your comment.
With docker logs container-name I was able to find out, that I got a way more detailed error message.
It says:
Error: Database is uninitialized and superuser password is not
specified.
You must specify POSTGRES_PASSWORD for the superuser. Use
"-e POSTGRES_PASSWORD=password" to set it in "docker run".
You may also use POSTGRES_HOST_AUTH_METHOD=trust to allow all connections without a password. This is not recommended. See
PostgreSQL
documentation about "trust":
https://www.postgresql.org/docs/current/auth-trust.html`
This makes it clear for me, that I have to somehow preconfigure the container before I can start it.

Related

Running a Chainlink Node - Remote DATABASE_URL Config PostgreSQL problem

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

Docker Postgres Image not accessible

So, I pulled the postgres image down from docker. I followed a tutorial which explained what's going with the command below and the the whole docker pull. I can log in to the instance fine. But when I restart my computer or shutdown docker I end up goign through similar setup steps and am not able to access the postgres instance anymore. Can someone explain what's going on here:
Run this command
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker -d postgres -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres
log in via PG admin.
Nothing, instance not available.
So, I feel like I am missing a step at one point I had executed a command like this:
docker exec -it c5b8bdd0820b35a01ea153a44e82458a6285cf484b701b2b2d6d4210266fb4f8 bash
which gave me acess to the shell for the image, after doing that I was able then to use PGAdmin, however, I feel like that may have been coincidence? As this does not work currently.
So, what am I doing wrong? What's an easier way to do this?
The --rm causes Docker to automatically remove the container when it exits. Remove it.
You can also add --restart always and your container will be up after restart.

Pushkin fails to initialize PostgreSQL database

I am following the Pushkin Quickstart guide.
At pushkin init site, I get an error that no container was found for test_db_1:
...
Pulling test_db (postgres:11)...
11: Pulling from library/postgres
Digest: sha256:8e096175da9b7a1d5f073e4ff0b2058a68b3110dc9c26bcee0975d25ad1c008e
Status: Downloaded newer image for postgres:11
Creating pushkin_test_db_1 ... done
Creating pushkin_message-queue_1 ... done
Creating pushkin_api_1 ... done
Creating pushkin_server_1 ... done
Starting test_db ... done
Creating local test database
No container found for test_db_1
Failed to run create database command in test_db container: 1
If I open Docker Desktop > pushkin > pushkin_test_db_1 > Logs, I see
Error: Database is uninitialized and superuser password is not specified.
You must specify POSTGRES_PASSWORD to a non-empty value for the superuser.
For example, "-e POSTGRES_PASSWORD=password" on "docker run".
You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all connections without a password.
This is *not* recommended. See PostgreSQL documentation about "trust":
https://www.postgresql.org/docs/current/auth-trust.html
I tried opening ./pushkin.yaml, adding a password there, and running pushkin init site but nothing has changed. My guess is that the command represented by Creating pushkin_test_db_1 doesn't provide Docker a password to the database. Looking on the postgres github, this behavior seems to be a new "feature."
Does anyone have a recommendation to fix this issue?
I think I found a fix. First, I deleted all of the old docker containers to prevent conflicts when trying to create new ones. Then, from a new directory, I ran pushkin site default to initialize the filesystem. Next, in ./pushkin/docker-compose.dev.yml, update services: test_db to look like the following:
test_db:
image: 'postgres:11'
environment:
POSTGRES_PASSWORD: testpassword
ports:
- '5432:5432'
volumes:
- 'test_db_volume:/var/lib/postgresql/data'
This will provide a password for the postgres database so that it doesn't complain. It looks like this could be easily updated at the source so that when pushkin site default downloads temp.zip, this change is already implemented in the yaml file.
This is a synchronization problem with the CLI. Sometimes, simply running "pushkin init site" again works. Try that and let me know if you get a different error.
What the pushkin init site command does is download all the dependencies the pushkin project needs. You may want to continue the following steps on the quick start and see if things go well.
And for this issue, try to run docker-compose -f pushkin/docker-compose.dev.yml exec -T test_db psql -U postgres -c "create database test_db and docker-compose -f pushkin/docker-compose.dev.yml stop test_db to see if anything change.

Overriding a single configuration in a Postgres Docker container

I'm trying to override a single item in the Postgres configuration of the official Docker Postgres image. Namely, I want to override the log_statement property and set it to all.
Tried it with:
docker run -d -e POSTGRES_PASSWORD=postgres postgres -c 'log_statement=all'
After enter the docker container and execute:
cat /var/lib/postgresql/data/postgresql.conf | grep log_statement
is still get the default value which is none.
but without success.
There are a few answers/questions regarding Postgres Docker configuration but they suggest replacing the complete postgresql.conf file.
From the docs:
Command-line options override any conflicting settings in postgresql.conf. Note that this means you won't be able to change the value on-the-fly by editing postgresql.conf, so while the command-line method might be convenient, it can cost you flexibility later.
As far as I understand that doesn't change the postgresql.conf but it actually runs the postgres command with the desired options. That's probably why you don't see the value set to all in the configuration file.
Hope it helps.
My solution is to mount Postgres config file somewhere on the server and edit it.
I test your question in my pc.
In the postgresql.conf log_statement="none" is commented.
Go manually and discomment it and it solve your problems.
docker exec -it postgres bash
Into the container:
apt update
apt install vim
vim /var/lib/postgresql/data/postgresql.conf
search what do you are looking for and change it manually.
Stop/start the container.
That is my solution.

How can i persist my data in docker/postgres container?

I know there are probably many ways to do this. What I am looking for is a way to do it using (preferably) only my DockerFile and one container.
Here is my current dockerfile:
FROM postgres:latest
ENV POSTGRES_USER=myuser
ENV POSTGRES_PASSWORD=mypassword
Here is the command I used to build this container:
docker built -t my_db .
And here is the command that I use to run the container:
docker run -p 5432:5432 my_db
What I would like to do is have the data stored in the container if possible, but I don't seem to understand how or where postgres stores it's data. I saw on another stack overflow post that postgres will store it by default in /var/lib/postgresql/data however when I look in that folder I see nothing. I can however verify that postgres is running because I am using a client called teamSQL and from that client I can create tables and insert/read data.
I can also verify that when i stop the container and restart the data is definitely not persisted.
Note: this is running in OSx but I don't think that is relevant.
You should use Docker volumes, so when you stop your container, data will persist on host machine, and when you start container again data will be mounted to it
docker volume create pgdata
docker run -p 5432:5432 -v pgdata:/var/lib/postgresql/data my_db