I'm reading about it and trying to solve it by myself:
ECONNREFUSED for Postgres on nodeJS with dockers
Unable to connect to Postgres DB due to the authentication type 10 is not supported
Unable to connect to Docker Postgres from outside
I did a lot of steps and I don't achieve a connection from my python script to Postgres Docker:
My docker-compose.yml
version: "3.8"
services:
postgres:
image: postgres:14
restart: always
ports:
- "5432:5432"
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root
- POSTGRES_DB=project1
pgadmin:
image: dpage/pgadmin4
environment:
- PGADMIN_DEFAULT_EMAIL=admin#admin.com
- PGADMIN_DEFAULT_PASSWORD=adminadmin
ports:
- "5050:80"
restart: always
depends_on:
- postgres
Then I run
docker-compose up
My script.py:
import psycopg2
conn = psycopg2.connect(
host="host.docker.internal",
database="project1",
user="root",
password="root",
port=5432
)
print(conn)
# create a cursor
cur = conn.cursor()
# execute a statement
print('PostgreSQL database version:')
result = cur.execute('SELECT version()')
print(result)
When I try to connect to DB, the console triggers multiple errors if I change host:
host.docker.internal (I think I have to do something in postgresql.conf or pg_hba.conf, but I'm not sure):
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "host.docker.internal" (192.168.1.145), port 5432 failed: FATAL: no pg_hba.conf entry for host "192.168.1.145", user "root", database "project1", SSL off
localhost
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user "root"
0.0.0.0
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "0.0.0.0", port 5432 failed: Cannot assign requested address (0x00002741/10049)
Is the server running on that host and accepting TCP/IP connections?
BTW: Docker runs services correctly and I can connect to PGAdmin without problems.
Any ideas?
this is docker will be access by localhost as host, not host.docker.internal
conn = psycopg2.connect(
host="localhost",
database="project1",
user="root",
password="root",
port=5432
)
In future when you try to host container for your script in same docker-compose file then host will be postgres
conn = psycopg2.connect(
host="postgres",
database="project1",
user="root",
password="root",
port=5432
)
Related
So I have my strapi server, which should connect to the Docker container running my Postgres DB, but I get this error when I try to start the development server for the strapi component of my application.
no pg_hba.conf entry for host "127.0.0.1", user "admin", database "store", no encryption
error: no pg_hba.conf entry for host "127.0.0.1", user "admin", database "store", no encryption
This is what is within my pg_hba.conf file on my local
# "local" is for Unix domain socket connections only
local all all scram-sha-256
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all scram-sha-256
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
I also have a docker-compose.yml file which creates the postgres database, so I can connect the strapi server to it.
version: '3'
services:
database:
image: 'postgres'
container_name: nndesign
build:
context: .
target: nndesign
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: nuzhat
POSTGRES_DB: store
expose:
- 5432
ports:
- 5432:5432
volumes:
- ./scripts/postgres-data:/var/lib/postgresql/data
- ./scripts/admin_users.sql:/docker-entrypoint-initdb.d/admin_users.sql
This is what I have as environment variables so the strapi server can connect to the postgres db running in the docker container
export default ({ env }) => ({
connection: {
client: 'postgres',
connection: {
host: env('DATABASE_HOST', 'localhost'),
port: env.int('DATABASE_PORT', 5432),
database: env('DATABASE_NAME', 'store'),
user: env('DATABASE_USERNAME', 'admin'),
password: env('DATABASE_PASSWORD', 'nuzhat'),
ssl: env.bool('DATABASE_SSL', false),
},
},
});
I'm a bit confused on how to revolve this error. Been trying to Google and also use ChatGPT to find a solution, but to no avail.
Any help would be greatly appreciated.
Thank you.
I have a docker-compose file:
version: '3'
services:
db:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_DB: ayyy
POSTGRES_USER: letsgo
POSTGRES_PASSWORD: pwpwpwpw22
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: user#example.com
PGADMIN_DEFAULT_PASSWORD: pwpwpwpw1
ports:
- "5433:80"
(I changed the environment variables to not spoof my credentials)
However I am unable to connect to my Postgres server through pgAdmin. pgAdmin is using reverse proxy from port 5433 to my subdomain pgadmin.domain.com. I am also reverse proxying HTTP from pgAdmin's container into HTTPS. (I do not know if that could be an issue)
This is the error I get:
Unable to connect to server:
connection to server at "db" (192.168.32.3), port 5432 failed: timeout expired
If I use localhost as a hostname, I get this:
Unable to connect to server:
connection to server at "localhost" (127.0.0.1), port 5432 failed:
Connection refused Is the server running on that host and accepting
TCP/IP connections? connection to server at "localhost" (::1), port
5432 failed: Address not available Is the server running on that host
and accepting TCP/IP connections?
UPDATE: I am not even able to ping the containers between each other. The service name is correctly resolved to IP, but I get no response.
I was even unable to open ports on my server, so I reinstalled it. Everything works as it should since then.
I have three docker containers (postgresql, adminer, and go/migrate) and I've exposed both adminer and postgres ports to the host. I can access adminer in my browser, and postico can also connect to the DB. When I try to connect to the db from within adminer, it throws this error:
SQLSTATE[08006] [7] 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: Address not available
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
The migrate container throws this error too:
error: dial tcp: 127.0.0.1:5432: connect: connection refused
So, clearly there is an issue with how the containers are able to communicate with each other. Do I need to create a docker network?
version: '3.1'
services:
db:
image: postgres
environment:
POSTGRES_DB: mydbname
POSTGRES_USER: mydbuser
POSTGRES_PASSWORD: mydbpwd
ports:
- "5432:5432"
migrate:
image: migrate/migrate
volumes:
- .:/migrations
command: ["-database", "postgres://mydbuser:mydbpwd#localhost:5432/mydbname?sslmode=disable", "-path", "/migrations", "up"]
links:
- db
adminer:
image: adminer
restart: always
ports:
- "8081:8080"
depends_on:
- db
You do not need to create linking, docker-compose create default network. also service to service communication should use service name, localhost mean this container(migrate) not DB container.
change localhost to db
migrate:
image: migrate/migrate
volumes:
- .:/migrations
command: ["-database", "postgres://mydbuser:mybpwd#db:5432/mydbname?sslmode=disable", "-path", "/migrations", "up"]
as you DB service name is db so you connect with db container using it name.
I'm trying to use Postico to connect to a docker postgreSQL container on my local machine.
I've tried connecting to 0.0.0.0, localhost, and 127.0.0.1. Each give me the following error:
could not connect to server: Connection refused
Is the server running on host "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?
0.0.0.0 gives me a similar, but smaller error:
could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
Here is my docker-compose file:
version: '3'
services:
prisma:
image: prismagraphql/prisma:1.23
restart: always
ports:
- "4466:4466"
environment:
PRISMA_CONFIG: |
port: 4466
databases:
default:
connector: postgres
host: postgres
port: 5432
user: prisma
password: prisma
migrations: true
postgres:
image: postgres:10.5
restart: always
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: prisma
volumes:
- postgres:/var/lib/postgresql/data
volumes:
postgres:
Solution found thanks to Egor! I forgot to specify ports: - "5432:5432" inside my docker-compose file. Rookie mistake ;)
I also had issues using Postico to connect to my Postgres DB in a docker container.
Ultimately, my issue was that I had a local Postgres DB running.
As soon as I disconnected my local Postgres DB, I was able to use Postico to connect to my docker DB. With the host set to localhost, I used the POSTGRES_USER, POSTGRES_PASSWORD, and host port as defined in my docker-compose.yml file.
If postgres version doesn't matter, try to change Postgres image to this one, it works for me
And also make sure that you add ports in docker-compose.yml
postgres:
image: postgres
restart: always
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: prisma
ports:
- "5432: 5432"
volumes:
- postgres:/var/lib/postgresql/data
P.s. just updated answer for readability
I'm using the following docker-compose.yml file:
version: "3.5"
services:
db:
image: postgres:latest
volumes:
- ./tmp/postgresql/:/var/run/postgresql/:rw
ports:
- 5432:5432
environment:
POSTGRES_DB: dev
POSTGRES_USER: username
POSTGRES_PASSWORD: pw
I can connect to the postgres instance from my host as I have 5432 port forwarded to the host. I wanted to try connecting via the socket, but I am hitting issues. I'm not sure if this is possible?
From my host I can use the following:
psql --host=/Users/jalbert/Projects/postgres-sockets/tmp/postgresql -U username -d dev`
psql: could not connect to server: Connection refused
Is the server running locally and accepting
connections on Unix domain socket "/Users/jalbert/Projects/postgres-sockets/tmp/postgresql/.s.PGSQL.5432"?
Although, I do see the socket file present there. If I go within the docker container I can connect via the socket no problem using a similar command.
Am I missing something to allow the host to use the socket connection? Unfortunately, I am not too familiar with the socket technologies.