How do I connect to host.docker.internal postgres instance? - postgresql

I'm trying to connect to my local server. I have the following docker-compose.yaml and servers.json files. I think I'm making a mistake with my .pgpass. When the containers are up and running I can login to pgAdmin, and I can see the docker_postgres_group "group", but when I try to login I get the authentication error in the first screenshot below. Further below is how I've got my pgpass set up. I think this is where the problem is, but I could be wrong...
docker-compose.yaml
version: '3.8'
services:
db:
container_name: pg_container
image: postgres
restart: always
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: test_db
ports:
- "5432:5432"
pgadmin:
container_name: pgadmin4_container
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: root
ports:
- "5050:80"
volumes:
- ./servers.json:/pgadmin4/servers.json # preconfigured servers/connections
- ./pgpass:/pgpass # passwords for the connections in this file
servers.json
{
"Servers": {
"1": {
"Name": "docker_postgres",
"Group": "docker_postgres_group",
"Host": "host.docker.internal",
"Port": 15432,
"MaintenanceDB": "postgres",
"Username": "postgres",
"PassFile": "/pgpass",
"SSLMode": "prefer"
}
}
}
.pgpass
This is what's inside my pgpass file: host.docker.internal:15432:postgres:postgres:postgres
Here's its location:
I've tried having the .pgpass file in the pgpass folder and outside.
And - I'm using postgres as the password to try to login to pgadmin.

When spinning up containers through docker-compose, also a network is created where each service can see the other services of the docker-compose definition. They are also resolvable by there servicename. In your case you can replace host.docker.internal with db and use the internal port 5432.
EDIT: host.docker.internal resolves the internal IP address used by your host machine. If you want connect through the host IP, the you have either to change the port in servers.json from 15432 to 5432 or the port mapping from the db service:
ports:
- "15432:5432"

Related

How can I connect to database running in docker-compose and deployed with ec2 instance?

I am new to Amazon AWS and have launched my first project. It is a simple django project, with postgres as a db, everything connected with the help of docker-compose. Here is the docker-compose file:
version: "3"
services:
backend:
build: ./
container_name: ${PROJECT_NAME}
working_dir: /${PROJECT_NAME}
ports:
- ${LOCAL_PORT}:${WSGI_PORT}
volumes:
- static_content:/${PROJECT_NAME}/src/static/
- media_content:/${PROJECT_NAME}/src/media/
- ./src:/${PROJECT_NAME}/src
restart: always
env_file:
- .env
postgres:
image: postgres:12.0
container_name: postgres
ports:
- "5432:5432"
volumes:
- local_postgres_data:/var/lib/postgresql/data
- local_postgres_data_backups:/backups
restart: always
env_file:
- .env
pgadmin:
image: dpage/pgadmin4
container_name: postgres_admin
ports:
- "5000:80"
volumes:
- pgadmin_data:/var/lib/pgadmin
env_file:
- .env
restart: on-failure
depends_on:
- postgres
volumes:
static_content: { }
media_content: { }
local_postgres_data: { }
local_postgres_data_backups: { }
pgadmin_data: { }
I have successfully uploaded the project to my EC2 instance, but the problem is that I cannot access the db with pgadmin. I tried connecting with the port, like this ec2-**-***-***-**.compute-1.amazonaws.com:5000, but it didn't work. Connecting with pgadmin app also didn't work as it gives me Unable to connect to server: connection to server at "***", port 5432 failed: timeout expired.
Security groups are as following:
And here is the DB config:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "course_project",
"USER": "postgres",
"PASSWORD": "admin",
"HOST": "postgres",
"PORT": "5432",
}
}
The thing is that postgres and pgadmin containers starts after running docker-compose up in my ec2 instance. However, I can`t access them…
Are there any lifehacks on how can I finally connect to my database?
Would be very grateful for every answer!
Can access to pgadmin? Or u can't connect to database via pgadmin ?
First ensure pgadmin is up and running
ssh youUser#ec2-**-***-***-**.compute-1.amazonaws.com
Ensure pgadmin container is up
docker ps | grep pgadmin
Ensure pgadmin gives response (port configuration is correct)
curl -v http://localhost:5000
should respond main page into console
At this moment you could try
ssh -L 5000:localhost:5000 ec2-**-***-***-**.compute-1.amazonaws.com
Now open on you local machine http://localhost:5000
If you able to see pgadmin console means everything correct. Only need to permit access on port 5000 to ec2----.compute-1.amazonaws.com inbound rule.
Anyway since you are using http better either use ssh passthrough(uppers example with -L), either enable tls in pgadmin.

How to connect cvat postgres db to Dbeaver

Docker container for cvat_db has following settings:
services:
cvat_db:
container_name: cvat_db
image: postgres:10-alpine
restart: always
environment:
POSTGRES_USER: root
POSTGRES_DB: cvat
POSTGRES_HOST_AUTH_METHOD: trust
volumes:
cvat_db:/var/lib/postgresql/data
networks:
cvat
While below is the connection setting in dbeaver, where "HOST IP" i have put the IP address where cvat is hosted.
Dbeaver Settings
I'm getting error of timeout connection. So, I want to know how to connect postgres database to dbeaver.
Keep the following in mind:
Postgres always need a password according to their docs.
Do not create custom networks if it is not really needed. Use the default bridge network instead.
Do you connect with Postgres from another docker container or from your host system? If you connect from your host system add ports with 5432:5432.
mount your volumes to a subpath instead of named volumes
Example compose file:
version: '3.9'
services:
cvat_db:
container_name: cvat_db
image: postgres:10-alpine
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: password
POSTGRES_DB: cvat
volumes:
- ./cvat_db:/var/lib/postgresql/data
I wrote an article about docker compose networking, perhaps it helps.

Authentication failed when logging in to Postgres database created via docker-compose

I have set up the following docker-compose.yml file to set up and run PostgreSQL and PgAdmin.
version: '3.1'
services:
db:
image: postgres:latest
container_name: postgres-dopp
restart: unless-stopped
environment:
POSTGRES_USER: dopp_dev
POSTGRES_PASSWORD: dopp_dev_pass
PGDATA: /data/postgres
ports:
- "5432:5432"
volumes:
- dbdata-dopp:/data/postgres
networks:
- network-dopp
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: pgadmin4#pgadmin.org
PGADMIN_DEFAULT_PASSWORD: admin
PGADMIN_CONFIG_SERVER_MODE: 'False'
depends_on:
- db
volumes:
- dbdata-dopp:/data/pgadmin
ports:
- "5050:80"
networks:
- network-dopp
networks:
network-dopp:
driver: bridge
volumes:
dbdata-dopp:
name: dopp-db-data
driver: local
This works fine, insofar as I can navigate to PgAdmin in my host machine's browser and through that I can connect to the database using the credentials I've defined in the environment variables. However, when attempting to make a direct connection to the postgres database from my host machine (by connecting to localhost:5432, since I have configured to expose that port), I then get the following error response:
[28P01] FATAL: password authentication failed for user "dopp_dev"
I'm fairly new to the peculiarities of Postgres and docker configuration, so I'm not sure what is causing Postgres to say that password authentication fails when connecting from my host machine, while it works perfectly fine if I do it through PgAdmin, which is on the same internal docker network.
Actually, I discovered that the docker postgres service's port 5432 was being shadowed by a local postgres instance running my host machine.

How can I connect to docker Postgres server from PGAdmin4

I am new to Docker containers but not quite new to Postgres (I delved a bit in the past).
The thing is I am trying to connect to a Postgres server started from the following Docker container dpage/pgadmin4
The issue is that I cannot connect to the server using PGAdmin4.
I am getting the following error:
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?
I have the following yaml file:
version: '3'
services:
db:
image: postgres
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: parola
PGDATA: /var/lib/postgresql/data/pgdata
pgadmin4:
image: dpage/pgadmin4
ports:
- "5050:80"
environment:
PGADMIN_DEFAULT_EMAIL: user#domain.com
PGADMIN_DEFAULT_PASSWORD: parola
Using the psql command line I can connect to it only if I set -h 0.0.0.0, as follows:
psql -h 0.0.0.0 -U postgres
Can someone tell me what I need to set up in the yaml file or how I can configure so it allow connection from localhost/127.0.0.1?
I am running on Ubuntu 20.04.2, AMD64 with Docker version 20.10.7, build f0df350.
service pgadmin4 is not being able reach service db because inside pgadmin4 at port 5432 there wont be any service running
With this configuration, you are only mapping the db to host machine's 5432 port. That is why you were able to run psql -h 0.0.0.0 -U postgres in your machine (host machine)
According to docker-compose pgadmin4 can access DB using host name db
you can link the configuration with some additional configuration as below
version: '3'
services:
db:
image: postgres
restart: always
ports:
- "5432"
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: parola
PGDATA: /var/lib/postgresql/data/pgdata
pgadmin4:
image: dpage/pgadmin4
ports:
- "5050:80"
environment:
PGADMIN_DEFAULT_EMAIL: user#domain.com
PGADMIN_DEFAULT_PASSWORD: parola
volumes:
- ./servers.json:/pgadmin4/servers.json
add a file called servers.json in the same directory as docker-compose with the below contents. take note that I have added the Host as db
{
"Servers": {
"1": {
"Name": "local db",
"Group": "Server Group 1",
"Port": 5432,
"Username": "postgres",
"Host": "db",
"SSLMode": "prefer",
"MaintenanceDB": "postgres"
}
}
}

Cannot connect to postico from docker-compose postgresql service

I've done a docker-compose up and been able to run my web service attached to a postgresql image. Problem is, I can't view the data on postico when I try to access the database. The name of the image is db and when i try to specify hostname to be "db" on postico before i connect, i get an error saying hostname not found. I've entered my credentials, port and database name the same way i keyed them in my docker-compose file.
Does anybody know how i can find the correct setup to connect to within the container?
version: '3.6'
services:
phoenix:
# tell docker-compose which Dockerfile it needs to build
build:
context: .
dockerfile: Dockerfile.phoenix.development
# map the port of phoenix to the local dev port
ports:
- 4000:4000
# mount the code folder inside the running container for easy development
volumes:
- ./my_app:/app
# make sure we start mongodb when we start this service
# links:
# - db
depends_on:
- db
- redis
environment:
GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID}
GOOGLE_CLIENT_SECRET: ${GOOGLE_CLIENT_SECRET}
FACEBOOK_CLIENT_ID: ${FACEBOOK_CLIENT_ID}
FACEBOOK_CLIENT_SECRET: ${FACEBOOK_CLIENT_SECRET}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_USER: ${POSTGRES_USER}
go:
build:
context: .
dockerfile: Dockerfile.go.development
ports:
- 8080:8080
volumes:
- ./genesys-api:/go/src/github.com/sc4224/genesys-api
depends_on:
- db
- redis
- phoenix
db:
container_name: db
image: postgres:latest
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_USER: ${POSTGRES_USER}
volumes:
- ./data/db:/data/db
restart: always
redis:
container_name: redis
image: redis:latest
ports:
- "6379:6379"
volumes:
- ./data/redis:/data/redis
entrypoint: redis-server
restart: always
use hostname as localhost.
You can't use the hostname db outside the internal docker network. That would work in the applications running in the same network.
Since you exposed the db to run on port 5432, it's exposed via 0.0.0.0:5432->5432/tcp and therefore is accessible with localhost as host and port 5432