I'm trying to create PostgreSQL DB inside docker container and connect to it from my local machine. Running docker-compose up -d with that inside docker-compose.yml:
version: '3.5'
services:
db:
image: postgres:12.2
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_DB: db
POSTGRES_USER: root
POSTGRES_PASSWORD: root
ended successfully. No crashes, errors of something. But, when I'm trying to connect to it with pgAdmin4 with these credentials:
Host name/address: localhost
Port: 5432
Maintenance database: db
Username: root
Password: root
it says to me:
Unable to connect to server:
FATAL: password authentication failed for user "root"
My OS: Windows 10 build(1809)
PostgreSQL version (installed on local machine): 12
Docker version: 19.03.13, build 4484c46d9d
UPD 1:
After re-creating container with different ports (now it is 5433:5433), pgAdmin4 error changed:
Unable to connect to server:
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
Host name/address: localhost
Port: 5432
You are trying to connect to 5432 port on localhost. Are you sure your container is taking the host IP?
To make the container run with the host IP run the container with --network host option.
docker run --network host <rest of the command>
Note that if you use '--network host' option, then portmapping '-p' option is not needed.
Read https://docs.docker.com/network/host/ for more information.
Have you checked you've cleaned away any old instances running locally and that you're not trying to access an old instance?
You can wipe out all local docker containers with: docker rm -f $(docker ps -aq)
Once you've got a clean environment you can try spin up the containers again locally and see if you can access the service. I copy/pasted what you have into a clean docker-compose.yaml and ran docker-compose up against the file - it worked and I logged in and was able to view the pg_user table.
If it still fails you can try to find the IP using: netstat -in | grep en0 which will show something like
en0 1500 192.168.1 **192.168.1.163** 15301832 - 9001208 - - -
this shows the external/accessible IP of the container. Try using the address shown (something similar to 192.168.1.163) instead of localhost
Related
I am trying to connect to a Postgres instance running in a Docker container. In the docker-compose file, the postgres service looks like this:
flask-api-postgres:
container_name: flask-api-postgres
image: postgres:13.4-alpine
env_file:
- dev.env
ports:
- "5433:5433"
networks:
flask-network:
With docker inspect I get that the container has the address: 172.19.0.2.
The API works fine, but when trying to access the database from Pgadmin with the config shown in the image (user and password are correctly set), I get the shown error.
Pgadmin config
I do not know how to access the postgres instance from pgadmin.
One approach is you can access the postgres db docker container from pgadmin which is hosted in your host machine using 127.0.0.1 instead of 172.19.0.2
Another way is you can create another container for pgadmin. In this case, you can access your PostgreSQL using container IP (For example: 172.19.0.2). Add this to your docker-compose file
pgadmin:
image: dpage/pgadmin4
depends_on:
- flask-api-postgres
ports:
- "5050:80"
environment:
PGADMIN_DEFAULT_EMAIL: pgadmin4#pgadmin.org
PGADMIN_DEFAULT_PASSWORD: admin
restart: unless-stopped
networks:
flask-network:
Make sure both are under same network.
Please check the port you are using. The default is 5432.
See experiment:
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c4d92a623a6 postgres:latest "docker-entrypoint.s…" 14 minutes ago Up 14 minutes 5432/tcp, 0.0.0.0:5433->5433/tcp cannot-access-postgres-instance-running-in-docker-container-from-pgadmin-database-1
> docker exec -it 0c4d92a623a6 sh
# psql "host=127.0.0.1 port=5433"
psql: error: connection to server at "127.0.0.1", port 5433 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
# psql "host=127.0.0.1 port=5432"
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: role "root" does not exist
#
I just started learning docker and I decided to create a postgresql container, and I want to use it as my database.
But the thing is, every time I tried to connect to my postgresql container with my Gui (PostBird), I get an error that says "Connection terminated unexpectedly".
My config file:
postgres:
image: postgres:alpine
restart: always
ports:
- '3000:3000'
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: adonisvue
volumes:
- ./init:/docker-entrypoint-initdb.d/
The command I used:
sudo docker-compose up
When my postgres container is running it says "database system is ready to accept connections" but I can't connect with my gui, even using connect url.
I had to change my port to 3000 because docker says that the port 5432 is already in use, but I don't have any container running in it. Is it because of psql?
Sorry, I'm really new to this and I just have a bunch of questions xD
I had to change my port to 3000 because docker says that the port 5432 is already in use, but I don't have any container running in it. Is it because of psql?
Yes, this is most probably because you have an other postgresql using this port on the local machine. Meanwhile, the postgres instance running inside your container is still using port 5432 (unless you also changed the port inside your container but I'm 99.9% sure you didn't). So the following port setting is wrong.
ports:
- '3000:3000'
This is mapping port 3000 for any IP configured on your host to port 3000 of your container... with no service running on that one. Try:
ports:
- '3000:5432'
You can then connect to postgres on port 3000 on your local machine which will forward packets to port 5432 inside the container.
I have the following definition of docker-compose file with postgres image that I want to run and connect to it from hosting machine of my PC, but I'm getting Connection Refused localhost:5432 all the time. I realize that container has to be run on host network driver, but network mode doesn't solve this problem. What I'm doing wrong?
I run this with docker-compose -f [file] up on Windows 10 with Docker for Desktop
version: '3.7'
services:
database:
image: postgres:10.6
restart: always
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=p0stgr#s
- POSTGRES_DB=eagle_eye_local
network_mode: host
When I run the same container with the following command it works:
docker container run --name postgres10.6 -e POSTGRES_PASSWORD=p0stgr#s -e POSTGRES_USER=postgres -e POSTGRES_DB=eagle_eye_local -p 5432:5432 postgres:10.6
I'm assuming you are not running natively on linux but use some Docker for Desktop. Then the short answer is: Remove the network_mode: host and the compose setup will work the same way your docker run command works.
version: '3.7'
services:
database:
image: postgres:10.6
restart: always
ports:
- "5432:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=p0stgr#s
- POSTGRES_DB=eagle_eye_local
The two examples you provided are not really equal even though they can lead to some similar results when run on a real linux host (similar in a way that on a linux host you will be able to access the postgres instance via localhost:5432 on the host machine).
If you run the given compose file on Docker for Desktop (Mac or Windows) you must keep in mind that in this case there is a VM running your containers and all commands are passed into that VM. If you don't use network_mode: host Docker will (1) expose the port correctly on the VM and (2) have some proxy process in place on your host machine (mac/windows) to forward the traffic into the VM. This basically doesn't work when you start the container with network_mode: host.
You can verify the established mapping when running docker ps under the Ports column. This will be empty if you run with network_mode: host.
For some more details see the discussions in the docker forum.
The main problem is that i cannot run postgresql even on vm with the error:
root#a2c8a58d4e0e:/# psql -h localhost -U psqluser -W
Password for user psqluser:
psql: 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?
For that purpose i run this commands inside VM:
pg_createcluster 9.6 main --start
/etc/init.d/postgresql start
And then it works properly on VM. But that's manually.
I configured everything by official docker repo docs.
This is my docker compose file:
version: "3.3"
services:
postgresql:
build:
context: .
dockerfile: postgresql
container_name: Postgres
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_DB: 'psqldb'
POSTGRES_USER: 'psqluser'
POSTGRES_PASSWORD: 'temp123'
volumes:
- /home/VOLUMES/DB/Postgresql:/var/lib/postgresql
I did inheritance from original repo as i want to run postgresql service automatically. Otherwise it's not running.
postgresql file:
FROM postgres:9.6.11
RUN pg_createcluster 9.6 main --start
RUN /etc/init.d/postgresql start
It does not run Postgres as well. Only manually inside VM.
What's wrong?
Within docker compose, ports aren't exposed on the host by default. https://docs.docker.com/compose/compose-file/
ports is virtual within the docker compose network. If you want to expose them to the host machine, you can use the expose option instead of ports.
Alternatively, you can also run docker-compose run with the --service-ports flag which will automatically expose the ports to the host when running.
docker-compose run --service-ports postgresql (see doc)
I have configured a production postgres sql database.
If I need to do debugging work, I don't want to be interacting with the production database or else that will affect the user base. Instead, I need to create a local environment such that nothing will be changed in the production database during debugging.
I am using Postgres SQL 10 and PGAdmin 4
How can I achieve that?
Thanks.
You could set up a test environment with docker.
first a docker-compose.yml file:
version: "3"
services:
db:
image: postgres:10-alpine
volumes:
- ./local_path:/var/lib/postgresql/data
ports:
- "8000:5432"
expose:
- "5432"
admin:
image: dpage/pgadmin4
environment:
- PGADMIN_DEFAULT_EMAIL=admin#admin.com
- PGADMIN_DEFAULT_PASSWORD=admin
ports:
- "8080:80"
See the documentation for the docker postgres image on how to set environment variables to define user/password/db name. https://hub.docker.com/_/postgres/
I'm not too familiar with pgadmin but container has minimal setup options:
https://hub.docker.com/r/dpage/pgadmin4/
Then you start the containers with sudo docker-compose up.
The db container is publishing its port on 8000 on your host machine, so there should be no conflict with the postgres server running on the host.
To connect:
psql -h localhost -p 8000 -U postgres
The admin page should be available at port 8080 on your host machine.
When you connect the admin to the database in the UI, the hostname is db and the port is 5432
Now that you have a docker container set up, you might also consider using it for production also :)