POSTGRES unable to connect with container - postgresql

I tried implementing Postgres with Odoo in Azure through docker compose, but I faced this error when I tried to check Odoo's container logs :
Database connection failure: connection to server at "db" (127.0.0.1),
port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections ?
Here's my docker-compose.yml
version: '3.2'
services:
db:
image: registryodoo.azurecr.io/samples/postgres:13
volumes:
- db:/var/lib/postgresql/data/pgdata
deploy:
restart_policy:
condition: always
expose:
- 5432
environment:
POSTGRES_PASSWORD: odoo
POSTGRES_DB: postgres
POSTGRES_USER: odoo
PGDATA: /var/lib/postgresql/data/pgdata
odoocontainer:
depends_on:
- db
image: registryodoo.azurecr.io/samples/odoo:latest
volumes:
- data:/var/lib/odoo
- extra-addons:/mnt/extra-addons
ports:
- 8069:8069
deploy:
restart_policy:
condition: always
environment:
HOST: db
USER: odoo
PASSWORD: odoo
volumes:
data:
driver: azure_file
driver_opts:
share_name: odoofileshare
storage_account_name: odooaccount1
db:
driver: azure_file
driver_opts:
share_name: odoofileshare
storage_account_name: odooaccount1
extra-addons:
driver: azure_file
driver_opts:
share_name: odoofileshare
storage_account_name: odooaccount1
After looking at some answers in related questions I tried with : expose : - 5432 , ports : - 5432 and not writing it the docker compose I still had the same issue.
Here's a screen of the container logs:
Azure container logs

Related

Connect to Postgres in DataGrip

I try to connect to my Postgres database built by docker in DataGrip but I get connection error.
Here is my application.yml file:
spring:
jpa:
database: POSTGRESQL
show-sql: true
hibernate:
ddl-auto: update
datasource:
url: jdbc:postgresql://db:5432/postgis_db?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true
username: postgres
password: postgres
Here is docker-compose.yml file:
version: '3.8'
services:
db:
image: postgres:latest
restart: always
volumes:
- db:/var/lib/postgresql/data
environment:
POSTGRES_DB: postgis_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
expose:
- 5432
networks:
- app-network
geolocation-service:
image: geolocation-service
build:
context: .
dockerfile: dockerfile
restart: always
ports:
- "8080:8080"
networks:
- app-network
volumes:
db:
networks:
app-network:
DataGrip connection settings:
And the error I get:
Does anybody knows how to solve this?
Connecting to this db by shell works fine:
docker exec -it a37 psql -U postgres postgis_db
Please check pg_hba.conf file, you have to allow connections from all hosts for this user, by default it is restricted to localhost.

Unable to launch docker postgres container

I am trying to create a docker which contains my existing postgreSQL database. Here is my docker-compose file :
version: "3.1"
services:
db:
restart: always
image: postgres
container_name: demo-postgres #you can change this
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=db
- POSTGRES_PORT=5432
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data/
volumes:
postgres_data:
ANd when I type :
docker-compose build
everything is ok but when I type that :
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
Could you help me please ?
Thank you very much !

How configure docker / pgadmin with nest

I'm trying to configure my docker to pgadmin4 in my nest js project, to do the backend, but when i try connect, show this message "Unable to connect to server:
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting"
I already tried configure the docker-compose.yml with pgadmin configurations, network configurations and
my docker-compose.yml
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: "12345678"
ports:
- "5432:5432"
volumes:
- ./data/pgadmin/
networks:
- pg-network
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: "monnerat#monnerat.com.br"
PGADMIN_DEFAULT_PASSWORD: "abc"
ports:
- "5050:80"
depends_on:
- db
networks:
- pg-network
networks:
pg-network:
driver: bridge
My actual result is connection refused is server running on host "localhost (127.0.0.1) and accepting"
but i expect he open the pgadmin to create the database
Your docker-compose.yml is good. Please see the attached host name should be 'db'

Connection Error accessing postgres docker container

I setup a prisma project recently and here is my docker-compose.yml file
version: '3'
services:
prisma:
image: prismagraphql/prisma:1.31
restart: always
ports:
- '4030:4466'
environment:
TZ: ${PRISMA_DB_TIME_ZONE}
PRISMA_CONFIG: |
port: 4466
# managementApiSecret: my-secret
databases:
default:
connector: postgres
host: postgres
port: 5432
user: prisma
password: ${PRISMA_DB_PASSWORD}
migrations: true
rawAccess: true
postgres:
image: postgres:10.3
restart: always
ports:
- "3306:3306"
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
TZ: ${PRISMA_DB_TIME_ZONE}
volumes:
- postgres:/var/lib/postgresql/data
volumes:
prisma:
postgres:
I can open my prisma playground and it functions without any issue. but I can't create a direct connection to the postgre container with dbeaver.
dbeaver Error message
Connection reset
Why my connection to the database fails?
This photo will be helpful.
postgres by default listens on port 5432.
In your postgres container specification, you should expose the port 5432 not 3306.
version: '3'
services:
prisma:
image: prismagraphql/prisma:1.31
restart: always
ports:
- '4030:4466'
environment:
TZ: ${PRISMA_DB_TIME_ZONE}
PRISMA_CONFIG: |
port: 4466
# managementApiSecret: my-secret
databases:
default:
connector: postgres
host: postgres
port: 5432
user: prisma
password: ${PRISMA_DB_PASSWORD}
migrations: true
rawAccess: true
postgres:
image: postgres:10.3
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
TZ: ${PRISMA_DB_TIME_ZONE}
volumes:
- postgres:/var/lib/postgresql/data
volumes:
prisma:
postgres:
If 5432 port in your host is already in use and if you want to use 3306 instead , then you can do port forwarding like below:
postgres:
image: postgres:10.3
restart: always
ports:
- "3306:5432"
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
TZ: ${PRISMA_DB_TIME_ZONE}
volumes:
- postgres:/var/lib/postgresql/data
UPDATE - 1
Reason why prisma can access postgres
ports section is meant only to make our services accessible in host level. But in the container level, If a port is open in a container, any other running container can access that port with the help of networks or links section.
By default, docker-compose will create a network per docker-compose.yml file and joins all the services in that file into that network.
That's the reason we could use the <service name> as the hostname and compose will resolve that name to the ip-address of the respective (in your case postgres) container.

docker springboot only connects on postgres 5432 via docker-compose

Using docker to connect springboot to postgres via docker-compose. Using port 5432 on postgres works fine, if i try an port other than that it fails
working code
spring
spring.datasource.url=jdbc:postgresql://db:5432/wwc
spring.datasource.username=wwc
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver
docker-compose
version: '2.1'
services:
db:
container_name: db
image: postgres:9.4
ports:
- 5432:5432
volumes:
- /tmp:/var/lib/postgresql
environment:
- POSTGRES_USER=wwc
- POSTGRES_DB=wwc
- POSTGRES_PASSWORD=test
server:
container_name: spring-boot-rest-server
build:
context: .
dockerfile: Dockerfile.server
ports:
- 8080:8080
logging:
driver: json-file
depends_on:
- db
web:
container_name: nginx-web
links:
- "server:springboot"
build:
context: .
dockerfile: Dockerfile.web
ports:
- 80:80
- 8088:8088
logging:
driver: json-file
depends_on:
- server
**connection refused code **
spring
spring.datasource.url=jdbc:postgresql://db:6000/wwc
spring.datasource.username=wwc
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver
docker-compose
version: '2.1'
services:
db:
container_name: db
image: postgres:9.4
ports:
- 6000:5432
volumes:
- /tmp:/var/lib/postgresql
environment:
- POSTGRES_USER=wwc
- POSTGRES_DB=wwc
- POSTGRES_PASSWORD=test
server:
container_name: spring-boot-rest-server
build:
context: .
dockerfile: Dockerfile.server
ports:
- 8080:8080
logging:
driver: json-file
depends_on:
- db
web:
container_name: nginx-web
links:
- "server:springboot"
build:
context: .
dockerfile: Dockerfile.web
ports:
- 80:80
- 8088:8088
logging:
driver: json-file
depends_on:
- server
error:
spring-boot-rest-server | org.postgresql.util.PSQLException: Connection to db:6000 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
What am i doing wrong?
You are confusing a bit the ports: your "db" container only exports 1 port: 5432. The 6000 that you put in your docker-compose is the port on localhost that you map to that container (db) on that port (5432).
You shouldn't even use the port mappings for the postgres container unless you want to connect from localhost which I guess you don't.
If you want to use another port than 5432 you need to extend the postgres Dockerfile and change the configuration so that postgres starts listening on a different port.
Hope this helps.
In other words: The port mapping configured in docker-compose has no relevancy to how the containers connect to each other. The mapping is only relevant when something/someone attempts to connect to your containers within the docker-compose from the outside. (Like from the localhost, as #Mihai remarked.)