Azure Data Studio connection error: FATAL: password authentication failed for user "postgres" - postgresql

I have created a PostgreSQL database inside a docker-compose container with the following yaml config:
version: '3'
services:
...
db:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_DB: my_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: my_password
volumes:
db:
driver: local
Without the volumes section, I was not able to access the database from outside the container, meaning that I was not able to add any data to it. With this added section, I am now able to run:
docker exec -it container_db_1 psql -U postgres
Which allows me to create databases, tables, add data, etc.
However I am now trying to connect to the database with Azure Data Studio but I get the error FATAL: password authentication failed for user "postgres". I've triple checked all the connection settings but I always get this same error.
In the past I was able to connect to a postgres database created through a docker container (on its own, not with docker-compose). And I don't understand what is different this time, since I can connect through the a terminal in the same way.

Related

weblate connect to aws rds postgres

I'm trying to connect weblate to external rds postgres database.
I'm using docker compose file that run weblate container. To this container I add the environment variables to connect to rds postgres.
The weblate container doesn't connect to rds postgres and give me this error:
psql: error: connection to server at "XXXX.rds.amazonaws.com", port 5432 failed: FATAL: password authentication failed for user "postgres"
but if I try to connect to rds postgres from inside the weblate container via cli, it works.
docker compose file:
version: '3'
services:
weblate:
image: weblate/weblate
tmpfs:
- /app/cache
volumes:
- weblate-data:/app/data
env_file:
- ./environment
restart: always
ports:
- 80:8080
environment:
POSTGRES_PASSWORD: XXXX
POSTGRES_USER: myuser
POSTGRES_DATABASE: mydb
POSTGRES_HOST: XXX.rds.amazonaws.com
POSTGRES_PORT: 5432
It tries to connect as postgres user while your configuration states myuser. Maybe the ./environment file overrides that?
I found the problem.
The problem was the char $ inside password.
Maybe the library used to connect to postgres has a bug or, simple, not allowed $ in password string.
When I removed that char it works.

Docker compose PostgreSQL 14.4 (latest) run 3 databases for multi-tenant system

My skill in Docker is little. I am using Docker desktop for Windows version 4.9.1 (81317) (latest version at this time). My web-app is multi-tenant system, need 3 databases. This is my docker-compose.yml (run 1 database ok)
version: '2.1'
services:
postgres:
image: postgres
ports:
- "5433:5432"
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: acc_spring
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d acc_spring"]
interval: 10s
timeout: 5s
retries: 3
Please guide me how to write/revise the above docker-composite.yml for running 3 databases at the same time:
tenant_master: port 5432, database name: tenant_master, username postgres, password postgres .
tenant_1: port 5432, database name: tenant_1, username postgres, password postgres .
tenant_2: port 5432, database name: tenant_2, username postgres, password postgres .
Notice: 3 databases in a database instance/server.
Please also explain for me what/how/ when to use restart: always, Do I have another option(s) for always?
Besides the port mapping (5433 seems wrong) you shouldn't need to change anything (well - probably the admin password shouldn't be 'postgres' in production).
You need to connect to this instance and create your databases (SQL or some app) and users. This is not related to Docker or docker compose. POSTGRES_DB is just the default database (not THE database).
However, if you want to do this as part of your container setup, you could do it like follows. Create a new project directory with these files:
File create-databases.sh
#!/bin/bash
set -eu
function create_database() {
local database=$1
echo " Creating database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $POSTGRES_USER;
EOSQL
}
create_database $POSTGRES_DB2
create_database $POSTGRES_DB3
File docker-compose.yml
services:
postgres:
image: postgres:latest
container_name: postgres
ports:
- "5433:5432"
restart: unless-stopped
volumes:
- ./create-databases.sh:/docker-entrypoint-initdb.d/create-databases.sh
- ./db_persist:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: tenant_master
POSTGRES_DB2: tenant_1
POSTGRES_DB3: tenant_2
While this is not best practice (I'm not a Postgres user) it will get you started. By using volumes, an init script is put into a folder where it is picked up by container initialization and the database is persisted, just in case. I left out the health-check for better clarity. It can be re-added of course.
Having said this, there would be another solution: just copy the postgres service (postgres2, postgres3) to be used with different ports (5434, 5435). Although this would run 3 independent database instances (needs more ressources), it might have its advantages as well.
As regards the restart policy: instead of "always" you might consider "unless-stopped" (see https://docs.docker.com/engine/reference/run/#restart-policies---restart) like it is done in my suggestion above.

Postgres and Docker Compose; password authentication fails and role 'postgres' does not exist. Cannot connect from pgAdmin4

I have a docker-compose that brings up the psql database as below, currently I'm trying to connect to it with pgAdmin4 (not in a docker container) and be able to view it. I've been having trouble authenticating with the DB and I don't understand why.
docker-compose
version: "3"
services:
# nginx and server also have an override, but not important for this q.
nginx:
ports:
- 1234:80
- 1235:443
server:
build: ./server
ports:
- 3001:3001 # app server port
- 9230:9230 # debugging port
env_file: .env
command: yarn dev
volumes:
# Mirror local code but not node_modules
- /server/node_modules/
- ./server:/server
database:
container_name: column-db
image: 'postgres:latest'
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres # The PostgreSQL user (useful to connect to the database)
POSTGRES_PASSWORD: root # The PostgreSQL password (useful to connect to the database)
POSTGRES_DB: postgres # The PostgreSQL default database (automatically created at first launch)
volumes:
- ./db-data/:/var/lib/postgresql/data/
networks:
app-network:
driver: bridge
I do docker-compose up then check the logs, and it says that it is ready for connections. I go to pgAdmin and enter the following:
where password is root. I then get this error:
FATAL: password authentication failed for user "postgres"
I check the docker logs and I see
DETAIL: Role "postgres" does not exist.
I'm not sure what I'm doing wrong, according to the docs the super user should be created with those specifications. Am I missing something? Been banging my head against this for an hour now. Any help is appreciated!
#jjanes solved it in a comment, I had used a mapped volume and never properly set up the db. Removed the volume and we're good to go.

PostgreSQL Container in Docker Not Authorizing a Correct Password

I have arranged a node.js back end to connect to a redis cache and psql database.
The app I have created is running but I would like to do some database admin and have attempted to log in using pgAdmin - however, my details were rejected.
I thought it might be a pgAdmin thing so I attempted to use the login URI in powershell but again it was rejected.
I checked that the psql service is running on the exposed port (in case I messed up the docker-compose config) and it is...not sure where to go from here.
My docker-compose config for the database is:
# PostgreSQL
postgres:
container_name: postgres
build: ./postgres
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
POSTGRES_URL: postgres://admin:password#localhost:5432/myapp
POSTGRES_DB: myapp
POSTGRES_HOST: postgres
ports:
- "5432:5432"
I should note that the database is running - I can log in to my front end and access data, etc...
My login attempt:
psql postgres://admin:password#localhost:5432/myapp
And the response:
psql: FATAL: password authentication failed for user "admin"
I think you docker-compose not formatted well if it's not copy-paste issue as the environment variable, not place properly.
# PostgreSQL
postgres:
image: postgres
container_name: postgres
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
POSTGRES_URL: postgres://admin:password#localhost:5432/myapp
POSTGRES_DB: myapp
POSTGRES_HOST: postgres
ports:
- "5432:5432"
Or you can try
version: '3.7'
services:
postgresdb:
container_name: postgres
environment:
POSTGRES_DB: appdb
POSTGRES_USER: appdb
POSTGRES_PASSWORD: 123123
image: bitnami/postgresql:latest
ports:
- "5432:5432"
Or better to post you Dockerfile, as I see your building your own Docker image, but better to use the offical image of Postgres like the one I posted above.
Also will suggest debugging on container DB first and verify connectivity on the container localhost, debugging and testing with depended containers like connecting from nodejs first here one lost in the actual problem.
Check if your ENV set properly.
docker exec postgres bash -c "printenv "
or
docker exec postgres bash -c "printenv | grep POSTGRES_"
or
docker exec -it postgres bash -c "psql -U admin myapp"

How do I create a database within a docker container using only the docker-compose file?

I'm trying to create a database and connect to it within my container network. I don't want to have to ssh into a box to create users/databases etc, as this is not a scalable or easily distributable process.
This is what I have so far:
# docker-compose.yml
db:
image: postgres:9.4
volumes:
- ./db/init.sql:/docker-entrypoint-initdb/10-init.sql
environment:
- PGDATA=/tmp
- PGDATABASE=web
- PGUSER=docker
- PGPASSWORD=password
This is my init.sql file:
CREATE DATABASE web;
CREATE USER docker WITH PASSWORD 'password';
GRANT ALL PRIVILEGES ON DATABASE web TO docker;
When I start up the container and try to connect to it, I get this error:
db_1 | FATAL: role "docker" does not exist
db_1 | done
db_1 | server started
db_1 | FATAL: database "web" does not exist
db_1 | psql: FATAL: database "web" does not exist
The first time this happened, I tried to create a role like this:
CREATE ROLE docker with SUPERUSER PASSWORD password;
GRANT web TO docker;
But it did not have any effect. To make matters even more confusing, when I use node-postgres to connect to the db, I get this error:
Error: connect ECONNREFUSED
But how can the connection be refused if the db service isnt even up??
In a nutshell, these are the questions I'm trying to solve:
How can I create a database using only the files in my project (i.e. no manual commands)?
How do I create a user/role using only the files in my project?
How do I connect to this database?
Thank you in advance.
How can I create a database using only the files in my project (i.e.
no manual commands)?
The minimal docker-compose.yml config for you defined user and database is:
postgres:
image: postgres:9.4
environment:
- POSTGRES_DB=web
- POSTGRES_USER=myuser
How do I create a user/role using only the files in my project?
To execute scripts on database initialization take a look at the official docs for initdb.
To get you started with a quick and dirty solution create a new file e.g. init_conf.sh in the same directory as your docker-compose.yml:
#!/bin/bash
set -e
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" <<-EOSQL
CREATE ROLE docker with SUPERUSER PASSWORD 'password';
EOSQL
And add the volumes directive to your docker-compose.yml.
volumes:
- .:/docker-entrypoint-initdb.d
Recreate your container because otherwise, you wouldn't trigger a new database initialization. That means, docker stop and docker rm the old one first before executing docker-compose up again. STDOUT gives you now some information about our newly introduced script.
How do I connect to this database?
To connect to your database with docker exec via the terminal:
docker exec -ti folder_postgres_1 psql -U myuser -d web
A docker-compose.yml in one of my production environments looks like the following:
services:
postgres:
logging: &logging
driver: json-file
options:
max-size: "10m"
max-file: "5"
build: ./docker/postgres # path to custom Dockerfile
volumes:
- postgres_data:/var/lib/postgresql/data
- postgres_backup:/backups
env_file: .env
restart: always
# ... other services like web, celery, redis, etc.
Dockerfile:
FROM postgres:latest
# ...
COPY *.sh /docker-entrypoint-initdb.d/
# ...
The environment variable you are using are wrong. Try this
version: '3.3'
services:
db:
image: postgres:9.4
restart: always
environment:
- POSTGRES_USER=docker
- POSTGRES_PASSWORD=password
- POSTGRES_DB=web
volumes:
- db_data:/var/lib/postgresql/data
# optional port
ports: ["5555:5432"]
volumes:
db_data:
then from any other docker-compose service you can access the DB at db:5432 and from your host machine you can access postgres on localhost:5555 if you also add the ports