Docker Compose Postgres always ended by timeout or connection refused - postgresql

Got a small question here. I suppose I've done something wrong at a moment but i can't find where and it's been +2 hours I'm turning around.
So Basically, I've created a docker-compose with Postgis (Postgres). I wanted to connect on it through Tableplus.
However, I can't ...
2 kind of error keep appearing :
When I try to connect basically on 127.0.0.1, it's keep telling me connection refused
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
When I try to use the docker IPAddress - 172.23.0.2 (docker inspect the image's ID to get the IP address of the image)
could not connect to server: Operation timed out
Is the server running on host "172.23.0.2" and accepting
TCP/IP connections on port 5432?
Here is my docker-compose.yml
version: '3.5'
services:
db:
image: kartoza/postgis:12.1
environment:
- POSTGRES_USER=user1
- POSTGRES_PASSWORD=password1
- POSTGRES_DB=database_db
volumes:
- data_db_volume:/var/lib/postgresql/12
ports:
- "5432:5432"
volumes:
data_db_volume:
At first, when I tried to connect, it was telling me: role user1 doesn't exist.
So to stop this I ran: brew services stop postgresql on my machine
I think a psql was running locally on the same port because with lsof -n -i:5432 | grep LISTEN i keep having information (it stop since I ran stop Postgresql)

Alright so after few days of research and trying on another computer, it seems it was coming from 2 points: the new docker software with a graphic interface that I didn't use before, once this running correctly and a prune done from this place everything started to work fine so I think something was causing error due to an outdated piece of software.
Thank's everyone

Related

Postgres/Docker : Can't connect to localhost:5432

I am running a postgres image using a docker-compose.yml file (not in detached mode). I have created two users and two databases there. In my bash terminal I logged into both users and when I typed \conninfo it said that
You are connected to database "db1" as user "user1" on host "localhost" (address "127.0.0.1") at port "5432".
and "db2" and "user2" for the other one.
When I typed the command "sudo netstat -plunt | grep postgres" it returned the following
tcp 0 0 127.0.0.1:5433 0.0.0.0:* LISTEN 953/postgres
I have a few questions.
1\ Why does it say port 5432 in the first one and 5433 in the second??
2\ When I type localhost:5432 in the url I don't see any page. It says
This page isn’t working
localhost didn’t send any data.
ERR_EMPTY_RESPONSE
Why can't I see anything in the web page at the above address??
3\ In my vscode terminal I am also getting these two error messages
2021-12-25 22:03:51.887 UTC [45] LOG: invalid length of startup packet
ERROR: relation public.databasechangeloglock does not exist at character 22
Do you know why I am getting these errors?
My docker-compose.yml file is as follows (shown only partly).
version: "3.4"
services:
sumo-db:
image: postgres-sumo:1.4.0
environment:
- POSTGRES_PASSWORD=password
- DB_NAME_1=db1
- DB_USER_1=user1
- DB_PASS_1=pass1
- DB_NAME_2=db2
- DB_USER_2=user2
- DB_PASS_2=pass2
volumes:
- sumo-db-data:/var/lib/postgresql/data
restart: on-failure
ports:
- 5432:5432
networks:
- sumo
If you need further information feel free to ask me. As always thank you.
Inshaf
If the docker hypervisor is capturing connections to 5432 on the host and tunneling them into the container, netstat will attribute this to the hypervisor process, not to a PostgreSQL process. netstat doesn't know who is on the other end of the tunnel, only its own end. Now it could recognize 5432 as being a traditional PostgreSQL port and label it that way, but -n tells it not to. Apparently you also have a 2nd instance of PostgreSQL, this one running directly on the host and on port 5433.
When I type localhost:5432 in the url I don't see any page.
A URL is something you can type, not something you type other things into. If you are putting that into the address bar of a web browser, that is not how you connect (successfully) to databases.
What is a vscode terminal? Does it have anything to do with either PostgreSQL or docker? Does the message mean that this terminal committed those errors, or someone else committed those errors against it?

How to connect to postgresql instance in with Docker Toolbox with pgadmin4 client

i'm new to Docker, and i'm running it with Docker Toolbox cause i have win 10 family.
I have a training project where there is a database to put up with docker-compose.
Here is the docker-compose.yml (it's given to me, but i can modify it).
version: '2'
services:
myerp.db:
image: postgres:9.4
ports:
- "127.0.0.1:9032:5432"
volumes:
# - "./data/db:/var/lib/postgresql/data"
- "./init/db/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d"
environment:
- POSTGRES_DB=db_myerp
- POSTGRES_USER=usr_myerp
- POSTGRES_PASSWORD=myerp
When i run that through Docker with docker-compose up it seems ok,
but i'm unable to connect to it through a server in pgadmin4
I'm not sure of the connexion setup i'm suppose to put because of Docker ToolBox, where in general to access the running instance you don't use 127.0.0.1 but 192.168.99.100 by defaut.
Anyway, if i configure a postgresql server with
Hostname: 127.0.0.1
Port: 9032
Username and password as written up, i have
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 9032?
If i change the IP in docker-compose for 192.168.99.100, i manage to connect,
but the database is not initialized although there are sql files in the folder
\init\db\docker-entrypoint-initdb.d :
01_create_schema.sql
02_create_tables.sql
21_insert__data_demo.sql
So is it my connexion that is wrongly setup ?
Or should i do it with 192.168.99.100, and it's the initialisation setup that is wrong in the docker-compose ?
Thanks for help !
Ok so i solve the problem, but unfortunately without Docker Toolbox.
I bought a key for windows 10 pro (6€), upgraded the system really fast, and installed Docker Desktop.
Everything worked instantly.
Before the connexion ip was indeed 192.168.99.100, but i don't know why the sql scripts where not copied in the virtual environment, and the folder
/docker-entrypoint-initdb.d
remained empty.
So my solution was to upgrade the system, and everything is fine !

Docker container, running PostgreSQL not allowing php's pg_connect from another container

I have the following architecture:
A network with 3 containers:
container_db, running PostgreSQL
container_pg_admin, running a simple pgAdmin environment
container_php_dev, running the whole environment, needed for the application to function properly (nginx, php, angular for the frontend, etc.)
When I try to pg_connect() from container_php_dev to container_db the connection gets refused. This is the actual error message I get: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: No route to host↵ Is the server running on host "container_db" (172.18.0.4) and accepting↵ TCP/IP connections on port 5432?.
However, when I try to connect to container_db from container_pg_admin I face no problem achieving this (host is container_db and port is 5432). Even if I map port 5432 of container_db to 5555 on host, I'm able to connect to 127.0.0.1:5555 from DBeaver or other DB Manager (TeamSQL).
All containers were restarted a few times as well as docker itself. And as this is my development machine, the host PC was restarted as well.
What might be the problem, how to diagnose and possibly solve? Have any of you experienced such strange docker behaviour?
Try running the container_db in the hosted network using flag --network host

How to connect from docker-compose to Host PostgreSQL?

I have a server with installed PostgreSQL. All my services work in containers (docker-compose). I want to use my Host PostgreSQL from containers. Buy I have the error:
Unable to obtain Jdbc connection from DataSource (jdbc:postgresql://localhost:5432/shop-bd) for user 'shop-bd-user': Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL State : 08001
Error Code : 0
Message : Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Connection refused (Connection refused)
My docker-compose is using host network_mode, like this:
version: '3'
services:
shop:
container_name: shop
build: ./shop
hostname: shop
restart: always
ports:
- 8084:8084
network_mode: "host"
My database connection URL is: jdbc:postgresql://localhost:5432/shop-bd
Updated Apri, 2018
According to the document, from version 18.03 onwards, using host.docker.internal as DNS works for me, without specifying neither --net=host or network_mode: "host"
This would have worked if you were on linux OS. But this won't work on Mac or Windows. The thing is that when you use --net=host, you still are not on host network. That is the limitation of how docker for windows work
Instead of localhost you need to use docker.for.win.localhost as the host. This is a special DNS name which is available on Docker for Windows when you want to refer to the host localhost
Cautionary note: using the --net=host, or network_mode: "host" will prevent your container from registering with Eureka and will break any kind of API gateway you've got unless they all run on the same network.
If you are mac user, use "host.docker.internal:5432" to connect to localhost Postgres, docs.
1) ensure that your postgresql instance is listening to all addresses and not only localhost.
In postgresql.conf set listen_addresses as follows:
listen_addresses='*'
2) you are trying to connect from your container to localhost:5432 . That is NOT your host's IP address, but the address of your container's loopback device. You need to use your docker bridge IP address instead.

PG::CONNECTION BAD connecting to pgAdminIII cloud 9 IDE

New to web development. Trying to connect and register new server for pgAdminIII. (using cloud 9 ide)..
I get this error when viewing in the browser..
_PG::CONNECTION BAD _
could not connect to server: Connection refused Is the server running on
"localhost" (::1) and accepting TCP/IP connections on port 5432?
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
PS. trying to access via new user "ubuntu" SUPERUSER created..
How can I fix this?
PPS. On cloud 9's community forum, an employee says..
"Only ports 8080, 8081 and 8082 are open for workspaces so if you want to access the DB externally you'll need to run it on one of these ports. Make sure you can access it with curl from a local terminal and ensure it's listening on 0.0.0.0 not 127.0.0.1, as the latter only allows local connections."
Lastly, their ide comes with postgres installed, all I needed to do, was set it up..
How do I use the above info while trying to register the new server in order to use the gui?
Let me know if I need to include anything else.
Thanks.
You should enable postgresql service by performing sudo service postgresql start.
After enabling, you can access the console using sudo sudo -u postgres psql and CREATE ROLE ubuntu LOGIN REPLICATION CREATEDB CREATEROLE SUPERUSER; to allow Rails take advantage of your database.
Rails also complains about template read error after creating ubuntu role.
To fix the template error, read up on this.
You should get PostgreSQL server up and running in no time. Happy coding!
Edit:
You should only use localhost for local and system resources (i.e. psql, etc.) while 0.0.0.0 is for everything else like running Rails or Apache server.