Connecting to Postgresql in a docker container with a GUI - postgresql

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.

Related

DataGrip Cannot Connect to Local PostgreSQL Running in Docker Desktop for Windows with WSL2 Backend

I have Docker Desktop installed on windows 10. It uses WSL2 back-end. I have 3 databases running on docker. One Mongo, One Clickhouse, and one PostgreSQL. DataGrip can easily connect to the Clickhouse on localhost:8123, and also to the Mongo on localhost:27017 but for some reason it cannot connect to the PostgreSQL running on 5432.
The peculiar thing about this is that pgAdmin can connect to the PostgreSQL on localhost:5432.
DataGrip can easily connect to the two other containers in this docker-compose file.
This is my docker compose, which I use to run the three containers:
version: "3.9"
services:
postgres:
image: postgres:15.1-alpine
restart: on-failure
ports:
- "5432:5432"
volumes:
- fpm_pg:/var/lib/postgresql/data
environment:
- POSTGRES_USER=postgres
- POSTGRES_DB=arm
- POSTGRES_PASSWORD=postgres
mongo:
image: "mongo:latest"
ports:
- "27017:27017"
env_file:
- .env
restart: "no"
clickhouse:
image: "clickhouse/clickhouse-server"
ports:
- "8123:8123"
- "9000:9000"
- "9004:9004"
depends_on:
- postgres
env_file:
- .env
restart: "no"
volumes:
fpm_pg:
driver: local
Error:
DBMS: Case sensitivity: plain=mixed,
delimited=exact
Driver: (ver. , JDBC)
Effective version: PostgreSQL (ver. 0.0)
The connection attempt failed.
Has anyone encountered this?
I also cannot establish this connection from within "Goland", which was the first thing I tried.
I did read this: DataGrip [08001] The connection attempt failed. The connection attempt failed, but it does not help.
OK. I solved it. After reading the log file myself, I noticed that it's a socket exception which led me to believe that there's something wrong with the port 5432. I still maintain that because pgAdmin could connect to that DB, DataGrip should have been able to do the same, but let's go down the rabbit hole shall we?
I used this video to inspect my ports:
How to find which application is using your TCP ports
I inspected the ports using:
netstat -an | findstr 5432
which showed me that even when the DB container is not running and even Docker itself is closed, something is listening on 5432.
I used:
netstat -aon | findstr 5432
I found out that a PID:4846 is running on 5432. Going into TaskManager>Details, finding PID 4846 led me to find out that "Windows IP Helper service" is listening on this port. A quick search led me to this answer:
Windows IP Helper Service (iphlpsvc) - is it possible to change port?
And I also remembered that for a previous project on an older version of docker in WSL2 which had a lot of problems forwarding ports, I had used port forwarding on this port. So, a quick:
netsh interface portproxy show all
netsh interface portproxy delete v4tov4 listenport=5432 listenaddress=0.0.0.0
netsh interface portproxy delete v4tov4 listenport=5432 listenaddress=127.0.0.1
solved the issue.
Now, I should again stress, that for some reason, pgAdmin didn't ahve any problem with the port forwarding and could connect to the DB with no problem, which led me to believe the port and connection should be fine. Hope this helps someone in the future.

Can only connect pgadmin to postgres docker container on port 5432, but not any other port?

I'm attempting to connect PGAdmin to a docker container and found this post (https://stackoverflow.com/a/57729412/11923025) very helpful in doing so. But I've tried testing using a port other than 5432 and am not having any luck.
For example, I tried using 5434 in my docker-compose file, and tried using that port in pgadmin but got the below error (This is the IP address found from using docker inspect)
This is what my docker-compose file looks like (I am using different ports for 'expose' and 'ports' on purpose, to try and narrow down which one will allow me to connect through PGAdmin, but am having no luck
database:
image: postgres:10.4-alpine
container_name: kafka-nodejs-example-database
environment:
POSTGRES_USER: "abcdef"
POSTGRES_PASSWORD: "abcdef"
expose:
- "5435"
ports:
- 8000:5434
pgadmin:
image: dpage/pgadmin4
ports:
- 5454:5454/tcp
environment:
- PGADMIN_DEFAULT_EMAIL=admin#mydomain.com
- PGADMIN_DEFAULT_PASSWORD=postgres
- PGADMIN_LISTEN_PORT=5454
Why is is that pgadmin has no issues with 5432 but when I ask it to use another port it throws this error?
I should note, the error in the screenshot above is from attempting to connect postgres container to a pgadmin container. I also tried connecting to the postgres container in my local application of pgadmin, and get a different timeout error below. I even get this same error for port 5432 when trying to connect using my local copy of pgadmin.
You exposed port 5434 of your container, but PostgreSQL itself is still configured to listen on port 5432. That is why you don't reach the database.
After running initdb and before starting PostgreSQL, configure the cluster, for example with
echo 'port = 5434' >> datadir/postgresql.auto.conf
But there should not be any need to start PostgreSQL on a different port. Just map port 5432 to 5434.
The PostgreSQL server listens on port 5432. Just changing things in Docker-level configuration doesn't change where the server process itself listens. That means:
The second number in ports: must be 5432. (The first number can be any number you want.)
Connections from other Docker containers must connect to port 5432. (ports: are ignored and aren't required.)
If you expose: a port, it must also be 5432. (This has no practical effect and duplicates EXPOSE 5432 in the Dockerfile.)
Since each container runs in an isolated network namespace, there's no particular reason to want to change this. You can run multiple database containers, and each will have its own separate container port 5432; they will not conflict.

Using Docker-compose, how to access the container database via the same port in container and in host

I'm running a docker-compose container for postgresql. The postgresql database in the container is running on the standard port 5432, and I am publishing that port out to port 5444 on the host (since the host's postgresql default port is in use).
I am using the same configuration on the host and in the container (a .env file that provides config settings for cli commands and the app as a whole). Unfortunately, whichever port I choose, one system will lose access. For example, I cannot run:
[k#host]$ pgsql -p 5444 # Connects
in the host and still have work inside the container:
[k#db-container]$ pgsql -p 5444 # Errors in container
The container's postgresql-server is running on 5432:
[k#db-container]$ pgsql -p 5432 # Connects successfully in container
and the ports are published via the docker-compose.yml via:
- ports:
- "5444:5432"
So currently I don't know how to configure the same port everywhere simply via the docker-compose.yml! expose command exposes the port but does not allow remapping, ports forwards host<-->container, but does not remap the internal port. I have thought of remapping the postgresql default port inside the postgresql container configuration, but fully reconfiguring postgresql seems non-trivial to do via docker-compose on every docker-compose up.
How can I remap the ports inside the container so that I can use port 5444 everywhere, in the host & container?
The standard PostgreSQL client library supports several environment variables that tell it where the server is. In the same way that you can configure the host using $PGHOST, you can configure the port using $PGPORT.
In a Docker Compose context, it should be straightforward to set these:
version: '3'
services:
postgres:
image: postgres:11
ports: ['5444:5432']
volumes: ['./postgres:/var/lib/postgresql/data']
myapp:
build: .
ports: ['8888:8888']
env:
PGHOST: postgres
# default PGPORT=5432 will work fine
Similarly, if you're running the application in a development environment on the host, you can set
PGHOST=localhost PGPORT=5444 ./myapp
You can't use the same port on the host.
Nothing prevents you to run multiple instances if they has different IP-addresses.
psql -p 5444
Defaults to psql --host=127.0.0.1 -p 5444. If you want several instances - obviously you must make them different in some way.

How to connect Postgresql Docker Container with another Docker Container

I want to connect mysoft docker container to postgresql docker container.
But i have some errors:
ERROR: for mysoft_db_1 Cannot start service db: driver failed programming external connectivity on endpoint mysoft_db_1 (XXX):
Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use
ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint mysoft_db_1 (XXX):
Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use
here is my docker-compose.yml
version: '2'
services:
mysoft:
image: mysoft/mysoft:1.2.3
ports:
- "80:8080"
environment:
- DATABASE_URL=postgres://mysoft:PASSWORD#db/mysoft?sslmode=disable
db:
image: postgresql
environment:
- POSTGRES_USER=mysoft
- POSTGRES_PASSWORD=PASSWORD
- POSTGRES_DB=mysoft
ports:
- 5432:5432
I want use another, already running docker pg server to connect new soft, also one pg docker server, for more projects
Is it possible?
You should add links to the definition of mysoft service in docker-compose.yml. Then your db service will be accessible from mysoft container.
After that your service definition will look like this.
mysoft:
image: mysoft/mysoft:1.2.3
ports:
- "80:8080"
environment:
- DATABASE_URL=postgres://mysoft:PASSWORD#db/mysoft?sslmode=disable
links:
- db
Now about error of binding. Probably, you receive it, because you have a local postgresql running on port 5432 or you already have a running docker container with 5432 port mapped to local machine.
ports:
- 5432:5432
It is used for mapping ports to your local machine. And if you don't need to access container's db from it, just remove it.
I want use another, already running docker pg server to connect new
soft, also one pg docker server, for more projects Is it possible?
Yes, it's possible. Use external_links.
If you choose this option:
Remove the db service and links in mysoft service definition from your docker-compose.yml
Add external_links with correct container name to mysoft service definition.
Update host and port in DATABASE_URL according to the container name and postgresql port in it.
You might want to check of you already have a local postgres running on port 5432? If you do you can not do the ports 5432:5432 but have to expose the inner port to an other outer port e.g. 5555:5432
at least if you are using native docker (running on localhost)...

Changing a postgres containers server port in Docker Compose

I am trying to deploy a second database container on a remote server using Docker compose. This postgresql server runs on port 5433 as opposed to 5432 as used by the first postgresql container.
When I set up the application I get this error output:
web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused
web_1 | Is the server running on host "db" (172.17.0.2) and accepting
web_1 | TCP/IP connections on port 5433?
and my docker compose file is:
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: route_admin
POSTGRES_USER: route_admin
expose:
- "5433"
ports:
- "5433"
volumes:
- ./backups:/home/backups
web:
build: .
command: bash -c "sleep 5 && python -u application/manage.py runserver 0.0.0.0:8081"
volumes:
- .:/code
ports:
- "81:8081"
links:
- db
environment:
- PYTHONUNBUFFERED=0
I feel the issue must be the postgresql.conf file on the server instance having set the port to 5432 causing the error when my app tries to connect to it. Is there a simple way of changing the port using a command in the compose file as opposed to messing around with volumes to replace the file?
I am using the official postgresql container for this job.
Some people may wish to actually change the port Postgres is running on, rather than remapping the exposed port to the host using the port directive.
To do so, use command: -p 5433
In the example used for the question:
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: route_admin
POSTGRES_USER: route_admin
expose:
- "5433" # Publishes 5433 to other containers but NOT to host machine
ports:
- "5433:5433"
volumes:
- ./backups:/home/backups
command: -p 5433
Note that only the host will respect the port directive. Other containers will not.
Assuming postgres is running on port 5432 in the container and you want to expose it on the host on 5433, this ports strophe:
ports:
- "5433:5432"
will expose the server on port 5433 on the host. You can get rid of your existing expose strophe in this scenario.
If you only want to expose the service to other services declared in the compose file (and NOT localhost), just use the expose strophe and point it to the already internally exposed port 5432.