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

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?

Related

Error: P1001: Can't reach database server at `localhost`:`5432`

I'm having a problem when running the npx prisma migrate dev command. Docker desktop tells me that the database is running correctly on port 5432 but I still can't see the problem.
I tried to put connect_timeout=300 to the connection string, tried many versions of postgres and docker, but I can't get it to work.
I leave you the link of the repo and photos so you can see the detail of the code.
I would greatly appreciate your help, since I have been lost for a long time with this.
Repo: https://github.com/gabrielmcreynolds/prisma-vs-typeorm/tree/master/prisma-project
Docker-compose.yml
version: "3.1"
services:
postgres:
image: postgres
container_name: postgresprisma
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=santino2002
ports:
- "5432:5432"
volumes:
- postgres:/var/lib/postgresql/data
volumes:
postgres:
Error:
Error: P1001: Can't reach database server at localhost:5432
Please make sure your database server is running at localhost:5432.
Docker ps show this:
Looks like the application and the database are running on two separate containers. So, in this case, connecting to localhost:5432 from the application container will try to connect to 5432 port within that container and not in the docker host's localhost.
To connect to database from the application container, use postgres:5432 (If they are on the same network) or <dockerhost>:5432.
Your docker ps output is showing that your postgres container has no ports connected to your local network.
It should look something similiar to this on ports column.
0.0.0.0:5432->5432/tcp, :::5432->5432/tcp
But yours is just 5432/tcp
You need to open ports for your postgres container.
Your docker-compose.yml file you posted in the question is correct. Probably you started postgres container with no ports first, then changed your docker-compose.yml file to have ports. So you just need to restart it now.
Use docker compose down && docker compose up --build -d to do that.

(Odoo) Why can't i connect to the server when using pganonymizer?

I am trying to anonymize some data in my database but I am getting the following Error.
Postgres is running on port 5432 in the container and I am exposing it on the host on port 5433.
ports:
- "5433:5432"
Am I supposed to add something in my odoo.conf file?
Thanks
The newest version of pganonymizer needs you to specify the postgres hostname and port in the subprocess command.
if env.get('PGHOST'):
cmd.extend(['--host', env['PGHOST']])
if env.get('PGPORT'):
cmd.extend(['--port', env['PGPORT']])

Docker Compose Postgres always ended by timeout or connection refused

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

how to interpret db volume entries in a docker compose file

I'm in the process of setting up my first postgresql docker container, as part of a distributed application using docker compose, and am somewhat confused on the syntax of the compose file. Still pretty new to docker so forgive me if this is straight out of docker 101.
Here's what it looks like for my postgres container in docker-compose.yml:
version: '2'
services:
database:
image: postgres
container_name: database-container-name
environment:
- POSTGRES_PASSWORD=some_password_here
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- PGDATA=/var/lib/postgresql/data/db-files/
ports:
- 5433:5432
volumes:
- ./.db/data:/var/lib/postgresql/data:delegated
- ./.db/init:/docker-entrypoint-initdb.d
The volumes bit is what throws me for a loop. Can someone explain what's going on there, is the container mapping its /.db/data/ folder to my local /var/lib/postgresql/data folder? I've looked at some documentation but it's not sinking in.
Also, in the ports section above, what's the deal with 5433:5432? Does that mean my port 5433 maps to the docker container's port 5432? If so, does this mean if I connnect to psql or pgAdmin on port 5433 on my box it's silently mapping to the postgresql instance in the docker container?
The volumes bit is what throws me for a loop. Can someone explain what's going on there, is the container mapping its /.db/data/ folder to my local /var/lib/postgresql/data folder?
In the volumes section of your database service, you're creating a couple of bind mounts between your host and your container. The syntax of each volume entry (in this situation) is HOST_PATH:CONTAINER_PATH:OPTIONS. So when you see:
- ./.db/data:/var/lib/postgresql/data:delegated
You are mapping the local (to your docker-compose.yml) .db/data directory onto /var/lib/postgresql/data in the container. In other words, changes made in one directory will be visible in the other.
The delegated option is specific to MacOS; the docs say:
delegated: The container runtime’s view of the mount is authoritative. There may be delays before updates made in a container are visible on the host.
The volume section of your service corresponds to the -v option to docker run; you may find more interesting in the docker run documentation in addition to the bind-mount docs I linked above.
Also, in the ports section above, what's the deal with 5433:5432? Does that mean my port 5433 maps to the docker container's port 5432? If so, does this mean if I connnect to psql or pgAdmin on port 5433 on my box it's silently mapping to the postgresql instance in the docker container?
The ports section is for publishing ports on your host. The syntax is HOST_PORT:CONTAINER_PORT. So when you see:
- 5433:5432
This is mapping host port 5433 to container port 5432. In other words, you can connect to your postgres database by connecting to port 5433 on your host. This is probably in place to avoid a conflict if you already had a postgres instance running on your host, which would already be bound to port 5432.

password authentication failed for user "postgres" with docker-compose up on EC2

On EC2 linux server create by docker-machine, when I launch docker postgres:10.6 by docker-compose up, I have these loop errors :
FATAL: password authentication failed for user "postgres"
DETAIL: Password does not match for user "postgres".
Connection matched pg_hba.conf line 95: "host all all all md5"
I don't have these errors if I start container manually
=> docker run -e POSTGRES_PASSWORD=myPassword postgres:10.6
I don't have these errors in my local docker.
My docker-compose :
db:
container_name: postgres
image: postgres:10.6
restart: always
environment:
POSTGRES_PASSWORD: myPassword
ports:
- "5432:5432"
Does anyone know how to fix this?
It might be because the volume (or bind mount directory) being already initialized after your first start. The postgres user, and database creation only happens on the first start (ie, /var/lib/postgresql/data must not already contain database files).
Try to run:
docker-compose rm -fv postgres to delete any containers or volumes (in particular).
docker-compose up -d to start your container.
Sorry for that I have the answer to my question, it's not a bug I just have something that tries to connect permanently to postgres (through port 5432 which is open) ...
After search, I think it's an attempt at hacking because incoming connections never come from the same IP
connection received: host=45.120.120.149.81 port=47118
connection received: host=210.4.125.252 port=44774
connection received: host=82.223.55.254 port=36320
etc....