How to move postgres into docker container? - postgresql

I usually attach to postgres by typing in the terminal:
psql -U user db
I want to move postgres into docker container:
docker run -v /var/lib/postgresql:/var/lib/postgresql -p 5432:5432 postgres
Then I run command and have an error:
psql -h localhost -p 5432 -U user
psql: FATAL: role "user" does not exist
I expected that -v /var/lib/postgresql:/var/lib/postgresql will make possible to reach db by user. But it doesn't happen. How to make it correctly?

Related

cannot authenticate with postgres

I'm trying to connect to running postgres container with psql:
docker pull postgres
docker run -e POSTGRES_PASSWORD=password -d postgres
C:\Program Files\PostgreSQL\13\bin>psql <myUserName>
Password for user <myUserName>:
at this point I type the given password, in this case just password and get the error
psql: error: FATAL: password authentication failed for user "<myUserName>"
What am I doing incorrectly ?
When you use docker run -e POSTGRES_PASSWORD=password -d postgres, the POSTGRES_PASSWORD would be set for user postgres as default. you can specify your user with POSTGRES_USER environment.
Second thing is that when you run a postgresql container and don't bind any ports for that, you can't connect to that container from outside. So you won't being able to connect to your container with pure pqsl command. Here you have 3 way to connect to your container:
1- Use docker run -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres to run container, then connect to it with psql -h <YOUR_IP> -p 5432 -U <USERNAME>
2- Get your container ip with docker inspect <CONTAINER_NAME> command, then connect to it with psql -h <CONTAINER_IP> -U <USERNAME>
3- Use psql inside your container with docker exec -it <CONTAINER_NAME> psql -U <USERNAME>

Is the password of postgresql installed with docker automatically changed?

I created the postgresql docker with the command below
docker run -d -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=1234 -v pgdata:/var/lib/postgresql/data postgres
But after some time, the following error occurs
[575] FATAL: password authentication failed for user "postgres"
[575] DETAIL: Password does not match for user "postgres".
Connection matched pg_hba.conf line 95: "host all all all md5"
Run the command below and it will work again
docker exec postgres su - postgres -c "psql -U postgres -d postgres -c \"alter user postgres with password '1234';\""
Can you tell me why this is happening?

why am I getting Postgres password authentication fails error?

I'm trying to learn and exercise with postgres.
I'm running on Ubuntu 16.04.
I installed Postgres docker and run it with the following command:
sudo docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker -d -p 5433:5433 -v $HOME/amitliron/UserA/docker/volumes/postgres:/var/lib/postgresql/data postgres
(I'm using 5433 port, becuse when trying to use 5432 port I'm getting the following error:
Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use, I think it's not need to be problem, but writing here everying I did)
I'm trying to check the postgres docker with the following command:
psql -h localhost -U postgres -d postgres
and entered the password: "docker" but I'm getting the following error:
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"
What am I missing ?
I needed to specify the port:
psql -h localhost -p 5433 -U postgres -d postgres

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.

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