invalid length of startup packet when trying to connect to Postgres - postgresql

My Postgres runs on docker, here is the docker-compose.yml:
services:
postgres:
container_name: postgres_dev
image: postgres
environment:
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-mypassword}
POSTGRES_DB: postgres
PGDATA: /data/postgres
volumes:
- postgres-dnb-dev:/data/postgres
ports:
- "127.0.0.1:5432:5432"
restart: unless-stopped
volumes:
postgres-dnb-dev:
I use Haproxy to define DNS, my DNS is dev.haproxy.local
When i try to connect to this DNS from my browser(another pc on the same network) i got 502 bad gateway and see in the docker container of postgres log:
PostgreSQL Database directory appears to contain a database; Skipping initialization
2023-02-15 10:10:45.162 UTC [1] LOG: starting PostgreSQL 15.2 (Debian 15.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2023-02-15 10:10:45.162 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2023-02-15 10:10:45.162 UTC [1] LOG: listening on IPv6 address "::", port 5432
2023-02-15 10:10:45.164 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2023-02-15 10:10:45.166 UTC [29] LOG: database system was shut down at 2023-02-15 10:10:39 UTC
2023-02-15 10:10:45.169 UTC [1] LOG: database system is ready to accept connections
2023-02-15 10:11:05.175 UTC [33] LOG: invalid length of startup packet
2023-02-15 10:11:05.243 UTC [34] LOG: invalid length of startup packet
It was working the day before, nothing changed on the network.
Does anyone know what is the solution can be?
thanks.

Related

Can't connect to Docker Hosted Postgres

My attempt to start a PostgreSQL Docker container fails when doing docker-compose. I can connect to it if I do docker run instead. I don't know what the difference is. My intent with the compose is to have it create a database usable for development and that I can connect to with user postgres.
docker-compose.yml:
version: '3.7'
services:
my_app:
build:
dockerfile: ./dockerfile-my-app
context: .
command: tail -f /dev/null
depends_on:
- db
env_file: ./.env.development
ports:
- "8080:8080"
volumes:
- /c/Users/woodsman/linux-mint-woodsman/home/dev:/home/dev
db:
image: postgres:latest
env_file: ./.env.development
environment:
- POSTGRES_USER= postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=helloworld
- PGDATA=/var/lib/postgresql/data/helloworld2/
ports:
- "5432:5432"
restart: "no"
volumes:
- /c/Users/woodsman/linux-mint-woodsman/dbdata:/var/lib/postgresql/data/pgdata
volumes:
dbdata:
Attempting to connect to jdbc:postgresql://localhost:5432/helloworld using user postgres and password postgres. Please pardon any security concerns you may perceive. This will be tightened for development, and done much more securely when deployed to production.
The reported error as reported by SquirrelSQL is:
hello-world: FATAL: password authentication failed for user "postgres"
class org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"
The image hashcode is:
9dbc24674f25eb449df11179ed3717c47348fb3aa985ae14b3936d54c2c09dde
>docker logs c4c2
2022-05-16 05:28:50.560 UTC [1] LOG: starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2022-05-16 05:28:50.560 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2022-05-16 05:28:50.560 UTC [1] LOG: listening on IPv6 address "::", port 5432
2022-05-16 05:28:50.564 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-05-16 05:28:50.571 UTC [63] LOG: database system was shut down at 2022-05-16 05:28:50 UTC
2022-05-16 05:28:50.579 UTC [1] LOG: database system is ready to accept connections
2022-05-16 05:29:17.184 UTC [70] FATAL: password authentication failed for user "postgres"
2022-05-16 05:29:17.184 UTC [70] DETAIL: Role "postgres" does not exist.
Connection matched pg_hba.conf line 100: "host all all all scram-sha-256"
2022-05-16 05:36:41.983 UTC [78] FATAL: password authentication failed for user "postgres"
2022-05-16 05:36:41.983 UTC [78] DETAIL: Role "postgres" does not exist.
Connection matched pg_hba.conf line 100: "host all all all scram-sha-256"
I hope for a Docker based answer. If there's some PostgreSQL command I need to do (or not do), please tell me how I can pass that through Docker.
Thanks,
Woodsman
Your main error is:
Role "postgres" does not exist.
It seems because whitespace here in docker-compose.yml:
POSTGRES_USER= postgres
In first start postgres was initialized, so when You change "POSTGRES_USER" in variable, role will not create. You can delete you volume and start docker-compose again.

Docker: could not create IPv6 socket for address "::": Address family not supported by protocol

I'm trying to start a postgres server using docker but it fails as soon as it starts. I believe it is something in the container and not on the host machine but I cannot find a configuration to use ipv4
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1 |
db_1 | 2022-04-12 20:25:50.331 UTC [1] LOG: starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2022-04-12 20:25:50.333 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2022-04-12 20:25:50.334 UTC [1] LOG: could not create IPv6 socket for address "::": Address family not supported by protocol
db_1 | 2022-04-12 20:25:50.340 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2022-04-12 20:25:50.349 UTC [26] LOG: database system was shut down at 2022-04-12 20:22:58 UTC
db_1 | 2022-04-12 20:25:50.354 UTC [1] LOG: database system is ready to accept connections
This is the compose file
# Use postgres/example user/password credentials
version: '3.1'
services:
db:
restart: on-failure
image: postgres
environment:
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
adminer:
restart: on-failure
image: adminer
command: php -S 0.0.0.0:8080 -t /var/www/html
ports:
- 8080:8080
I'm using
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.4 LTS
Release: 20.04
Codename: focal

Postgres database connectivity issues when using docker-compose

I am trying to dockerize the REST api written in golang, but having troubles connecting to postgres database (the app works fine without docker). Below are my Dockerfile and docker-compose.yml
FROM golang:alpine
RUN mkdir /app
ADD . /app
WORKDIR /app
COPY go.mod .
RUN go mod download
COPY . .
RUN go build -o main .
CMD ["/app/main"]
docker-compose.yml
version: '3'
volumes:
postgres_data:
driver: local
services:
postgres:
image: postgres
environment:
POSTGRES_DB: test_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 1234
ports:
- 5432:5432
my-go-app:
restart: always
build:
dockerfile: Dockerfile
context: .
environment:
# Postgres Details
DB_HOST: postgres
DB_DRIVER: postgres
DB_USER: postgres
DB_PASSWORD: 1234
DB_NAME: test_db
DB_PORT: 5432
# APP details
APP_PROTOCOL: http
APP_HOST: localhost
APP_PORT: 10000
ALLOWED_ORIGINS: "*"
ports:
- 10000:10000
depends_on:
- postgres
Database properties: host = "localhost", port = 5432, user = "postgres", password="1234", dbname="test_db" (all specified in docker-compose.yml). App itself runs on port 10000
When running docker up and docker-compose up --build I'm getting sql: database is closed error
Starting rest_db_my-go-app_1 ... done
Attaching to rest_db_postgres_1, rest_db_my-go-app_1
my-go-app_1 | 2021/04/02 12:54:07 Connection Failed to Open
my-go-app_1 | 2021/04/02 12:54:07 Starting development server at http://127.0.0.1:10000/
my-go-app_1 | 2021/04/02 12:54:07 Quit the server with CONTROL-C.
my-go-app_1 |
my-go-app_1 | (/app/main.go:185)
my-go-app_1 | [2021-04-02 12:54:07] sql: database is closed
postgres_1 |
postgres_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_1 |
postgres_1 | 2021-04-02 12:02:15.807 UTC [1] LOG: starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_1 | 2021-04-02 12:02:15.808 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2021-04-02 12:02:15.808 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2021-04-02 12:02:15.815 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2021-04-02 12:02:15.831 UTC [26] LOG: database system was shut down at 2021-04-02 12:02:14 UTC
postgres_1 | 2021-04-02 12:02:15.837 UTC [1] LOG: database system is ready to accept connections
postgres_1 | 2021-04-02 12:02:25.830 UTC [1] LOG: received fast shutdown request
postgres_1 | 2021-04-02 12:02:25.834 UTC [1] LOG: aborting any active transactions
postgres_1 | 2021-04-02 12:02:25.835 UTC [1] LOG: background worker "logical replication launcher" (PID 32) exited with exit code 1
postgres_1 | 2021-04-02 12:02:25.835 UTC [27] LOG: shutting down
postgres_1 | 2021-04-02 12:02:25.862 UTC [1] LOG: database system is shut down
How to solve this database connectivity issue? Thanks in advance

Docker: Adminer backend for PostgrSQL database times out

I'm trying to set up a simple database system using a PostgreSQL Docker container and an Adminer container as a backend. Note this is all running on a RaspberryPi 4.
docker-compose.yml:
services:
postgres:
image: postgres
restart: on-failure
ports:
- 5432:5432
env_file:
- ./.auth_file
volumes:
- db:/var/lib/postgresql/data
adminer:
image: adminer
restart: on-failure
ports:
- 8070:8080
env_file:
- ./.auth_file
depends_on:
- postgres
volumes:
db:
I then run it using docker-compose up:
postgres_1 |
postgres_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
postgres_1 |
postgres_1 | 2021-04-01 13:25:25.919 UTC [1] LOG: starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on arm-unknown-linux-gnueabihf, compiled by gcc (Debian 8.3.0-6) 8.3.0, 32-bit
postgres_1 | 2021-04-01 13:25:25.919 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2021-04-01 13:25:25.919 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2021-04-01 13:25:26.028 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2021-04-01 13:25:26.090 UTC [27] LOG: database system was shut down at 2021-04-01 13:20:41 UTC
postgres_1 | 2021-04-01 13:25:26.116 UTC [1] LOG: database system is ready to accept connections
adminer_1 | [Sun Jun 14 00:31:20 2071] PHP 7.4.16 Development Server (http://[::]:8080) started
adminer_1 | [Sun Jun 14 00:30:16 2071] [::ffff:192.168.16.1]:50886 Accepted
The last line shows that I tried to access localhost:8070 in the Browser right on the Pi. The page just loads forever and Adminer never returns the page.
Could this have to do with some firewall issues?
Thanks for any help!
Found the solution. This has to do with the Docker Container being unable to fetch the current time from the host, due to some version issues surrounding Docker, Alpine (the base image for Adminer) and libseccomp. This is also evident by the fact that Adminer shows entirely different timestamps than the Postgres-Container in my original post.
The full explanation can be found here: https://wiki.alpinelinux.org/wiki/Release_Notes_for_Alpine_3.13.0#time64_requirements
After following the steps linked above, the docker-compose.yml should look something like this:
adminer:
image: adminer
...
security_opt:
- seccomp=config/compose/default.json # required to avoid clock_gettime() errors
Replace config/compose/default.json with the relative path to the modified default.json.

Can't connect Go Application to the postgres container in docker-compose

I have been trying to connect my go application to postgresql db using docker-compose.
Here is my Dockerfile for the web server
FROM golang:latest
RUN mkdir /app
COPY . /app
WORKDIR /app
RUN go mod download
EXPOSE 5000
CMD ["go","run","main.go"]
And this is my docker-compose.yml file
version: '3'
services:
db:
image: postgres:latest
restart: always
network_mode: bridge
environment:
POSTGRES_USER: factly
POSTGRES_PASSWORD: qwertyui
POSTGRES_DB: factly
ports:
- "5432:5432"
server:
build: .
depends_on:
- db
ports:
- "5000:5000"
But I can't connect to the postgresql db as it gives a connection refused error message from the
Go serverStarting factly_db_1 ...
Starting factly_db_1 ... done
Recreating factly_server_1 ...
Recreating factly_server_1 ... done
Attaching to factly_db_1, factly_server_1
db_1 |
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1 |
db_1 | 2020-08-19 06:11:17.151 UTC [1] LOG: starting PostgreSQL 12.4 (Debian 12.4-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2020-08-19 06:11:17.151 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2020-08-19 06:11:17.152 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2020-08-19 06:11:17.266 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2020-08-19 06:11:17.906 UTC [26] LOG: database system was shut down at 2020-08-19 05:56:39 UTC
db_1 | 2020-08-19 06:11:18.213 UTC [1] LOG: database system is ready to accept connections
server_1 | host=127.0.0.1 port=5432 user=factly password=qwertyui dbname=factly sslmode=disable
server_1 | 2020/08/19 06:11:21 dial tcp 127.0.0.1:5432: connect: connection refused
server_1 | exit status 1
factly_server_1 exited with code 1
I have tried running just the postgres container and connecting it with the Go Application which works perfectly fine but running the Go Application inside a container gives the same error(Which makes me feel the Go web server container couldn't connect outside of it's network).
How can I correct this?
I guess the problem is you are trying to connect to 127.0.0.1 from the go program instead use the hostname of the database container db.