Connecting docker postgres to pgAdmin - postgresql

Given this docker-compose.yml, am experiencing difficulties connecting the docker stack to my pgAdmin.
version: '3.1'
services:
database:
image: postgres
restart: always
environment:
POSTGRES_USER: db-user
POSTGRES_PASSWORD: db-user
POSTGRES_DB: db
ports:
- 5432:5432
For the pgAdmin Connection properties, here's what I've used (others, default values):
Host: 127.0.0.1
Username & Password: db-user
And for the error message when saving:
Error saving properties: UNAUTHORIZED
Unable to connect to server

The docker postgresql page suggest the stack.yml:
# Use postgres/example user/password credentials
version: '3.1'
services:
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: example
adminer:
image: adminer
restart: always
ports:
- 8080:8080
And then visit http://localhost:8080.
Or:
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" ...
In your case, adding port to the database itself suggests your queries should use that port.

Related

How does Postgresql inside Docker work? psql: FATAL: password authentication failed for user xy

I created my app with .yml
services:
db:
image: postgres:11-alpine
environment:
POSTGRES_DB: wiki
POSTGRES_PASSWORD: quantoxrocks
POSTGRES_USER: wikijs
logging:
driver: "none"
restart: unless-stopped
volumes:
- db-data:/var/lib/postgresql/data
wiki:
image: ghcr.io/requarks/wiki:2
depends_on:
- db
environment:
DB_TYPE: postgres
DB_HOST: db
DB_PORT: 5432
DB_USER: wikijs
DB_PASS: quantoxrocks
DB_NAME: wiki
restart: unless-stopped
ports:
- "3000:3000"
webserver:
image: nginx:alpine
restart: unless-stopped
tty: true
ports:
- "443:443"
- "80:80"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- ./ssl:/etc/nginx/ssl
volumes:
db-data:
I logged in my db container and want to create database. I have tried at least 10 times and I am sure that password is from the above docker-compose.yml file. It does not work.
docker exec -it wiki_db_1 sh
Next
psql -h wiki_db_1 -U wikijs
Password for user wikijs:
psql: FATAL: password authentication failed for user "wikijs"
Why? How can I check any further logs?
The environment variables for Postgres are only used if there is no database present already when the container starts.
You have a volume mapping of /var/lib/postgresql/data and it's likely that you already have a database there, which was created with different values from the environment variable values.
If you don't have any important data in the existing database, you can delete the volume and Postgres will create a new database with the correct username/password.

Pgadmin does not start with docker-compose-file

I started working with postgres and discovered pgadmin.
I started like this:
Postgres:
$ docker run --name admin -e POSTGRES_PASSWORD=admin -p 5432:5432 -d postgres:latest
Pgadmin:
docker run -p 5050:80 -e "PGADMIN_DEFAULT_EMAIL=admin#admin.com" -e "PGADMIN_DEFAULT_PASSWORD=root" -d dpage/pgadmin4
So this worked perfectly fine, I started the postgres container and afterwarda the pgadmin cointainer and got on the site http://localhost:80/login and could login
The problem now is the docker-compose.yml that I wrote. As I deploy my docker-compose-file, the containers are both running but I can't access the login page of pgadmin
Docker-compose.yml:
version: '3.8'
services:
db:
image: postgres:latest
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin
ports:
- "5432:5432"
pgadmin:
image: dpage/pgadmin4:latest
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: root
ports:
- "5050:80"
When I run it, it works as it should. So I think your issue is the URL you try to access it on. You need to access it on the mapped port 5050. Not 80, as you've written. So the URL is http://localhost:5050/login.

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.

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.

Docker Postgres and Adminer accept connection but doesn't work

On my rasperry pi 4 I've installed docker and docker-compose and now I'm tring to install and use Postgres and Adminer
following that https://hub.docker.com/_/postgres I've created docker-compose.yaml file as follow:
# Use postgres/example user/password credentials
version: '3.1'
services:
db:
image: postgres
restart: unless-stopped
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
adminer:
image: adminer
restart: unless-stopped
ports:
- 8080:8080
and i run it with
docker-compose -f docker-compose.yaml up -d
after that DB_1 starts and adminer too
but when i try connect to http://192.168.1.38:8080/ i can't reach it
even if i try connect to postgres through pgAdmin it's says
could not connect to server: Connection refused (0x0000274D/10061) Is
the server running on host "192.168.1.38" and accepting TCP/IP
connections on port 5432?
however if i don't use docker-compose but just
docker run --name postgres -d --restart unless-stopped -p 5432:5432 -e POSTGRES_PASSWORD=123456 -v ${PWD}/data:/var/lib/postgresql/data postgres
it's work through pgAdmin
do you know what i'm doing wrong?
UPDATE: seems the problem is with docker-compose because any kind of docker-compose.yml file block connection to it...
with a container with djgango i tried to start server and it's works but when i try reach page it seem bloccked too
when i run docker-compose.yaml file docker-compose ps output is:
sudo netstat -tulpn screenshot
PgAdmin can't reach 5432 ports because you don't expose it.
Like for Adminer you need to expose the Postgres port 5432 on your machine in your compose file.
version: '3.1'
services:
db:
image: postgres
restart: unless-stopped
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
adminer:
image: adminer
restart: unless-stopped
ports:
- 8080:8080
a little late to the party but what you need to do is figure out the IP address of the postgres container, and use that as your host.