I have been spending 3-4 hours on this and still have not found a solution.
I can successfully run the docker container and use psql from the container bash, however, when I try to call the db from my local machine I continue to get this error message:
error role "postgres" does not exist
I have already tried editing "listen_addresses" in the postgresql.conf file from the container bash
My setup:
I am using a macbook - Monterey 12.4
my docker compose file:
version: '3.4'
services:
postgres:
image: postgres:latest
ports:
- "5432:5432"
environment:
- POSTGRES_DB=postgres_db
- POSTGRES_USER=testUser
- POSTGRES_PASSWORD=testPW
volumes:
- postgres-data:/var/lib/postgresql/db
but this issue occurs if I do it through the standard CLI command as well, i.e:
docker run -d -p 5432:5432 --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword postgres
I tried to follow this tutorial but it didnt work:
[https://betterprogramming.pub/connect-from-local-machine-to-postgresql-docker-container-f785f00461a7][1]
when I try this command:
psql -h localhost -p 5432 -U postgres -W
it doesnt work:
psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL: role "postgres" does not exist
Also for reference, the user "postgres" does exist in postgres - as a superuser
Replace POSTGRES_USER=testUser with POSTGRES_USER=postgres in the compose configuration. Also use the password defined in POSTGRES_PASSWORD. Delete the old container and create a new one.
Thank you all for your help on this.
It turns out the issue was that I was running postgres on my local machine as well.
so once I turn that off I was able to connect.
I appreciate your time!
Related
The problem
I am trying to connect to PostgreSQL from PhpStorm, but it returns the following error:
[28P01] FATAL: password authentication failed for user "app"
The situation
I have the following .env file setup:
POSTGRES_DB=app
POSTGRES_USER=app
POSTGRES_PASSWORD=password
POSTGRES_VERSION=15
And the following in docker-compose.yml:
version: '3'
services:
database:
image: postgres:${POSTGRES_VERSION}-alpine
container_name: database
env_file:
- .env
volumes:
- db-data:/var/lib/postgresql/data:rw
ports:
- '5432'
volumes:
db-data:
When running docker-compose up -d that does create a container & volume successfully.
So then I enter the following into my PhpStorm:
But then the error pops up, entering the password again doesn't fix anything.
I am running this on Ubuntu 22.04.1 LTS
What I've tried
I've rebuilt the container several times with different user & password combinations (making sure to use docker-compose down -v to get rid of the volume), all with the same result.
I've tried changing the password by executing docker exec -it database psql -U app and then running ALTER ROLE app WITH PASSWORD 'password', but this did not change anything.
I also saw online that it might have to do something with authentication of the user being setup as ident, but I cannot find a way to change this in the docker-compose.yml file.
The question
How could I set this up so I can connect my PhpStorm to the PostgreSQL database properly?
In my case I was running Windows 10 with WSL2 (Ubuntu). I had installed Postgres in the Ubuntu instance as part of setting up an app. I'd removed the app but the Postgres server was still running. When I attempted to connect to the Docker Postgres instance using localhost:5432 I was instead connecting to the WSL2 Postgres instance.
In my case, since I was no longer using Postgres in WSL2 I removed it and this resolved the issue. You could also stop it or use the host name/IP as mentioned by #jjanes
I'm running a docker container with the vanilla Postgres image on my local machine. I'd like to connect to the database from my local machine (i.e., not from "within the container". However, on trying to connect, I get an error.
Here's my docker-compose.yml file:
version: "3.8"
services:
db:
image: postgres
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: mypassword
Here's how I start up:
docker-compose run db
Here's how I connect:
psql -h localhost -p 5432 -U postgres
This produces the 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?
If I spin up the database without Docker Compose, the same connection command works as expected:
docker run --name mypg -p 5432:5432 -e POSTGRES_PASSWORD=password postgres
I could just go with the flow and use the command above. But this seems to be pointing to a flaw in how I think about Docker/Compose. For example, maybe Docker Compose's internal DNS resolver makes this approach fail.
Any ideas?
Version info:
psql --version
psql (PostgreSQL) 13.3
I have read through several SO posts, including these, but they don't address or fix the problem I'm seeing:
docker-compose: accessing postgres' shell (psql)
Can't connect to postgres when using docker-compose
Try docker-compose up db instead of run. Using run will run a one-off command against your container, whereas up will turn on the container and leave it running, so another application should be able to access it.
https://docs.docker.com/compose/faq/#whats-the-difference-between-up-run-and-start
Unsure if this is an issue with Docker for Mac, my Postgres installation (via Homebrew, or some other strange local issue going on.
Probably worth mentioning that I have never ran into this issue on Linux.
Here is a snippet from my docker-compose.yml file that uses the postgres image:
services:
postgres:
image: postgres
environment:
- POSTGRES_DB=some_db
- POSTGRES_USER=some_user
- POSTGRES_PASSWORD=supersecretpassword
ports:
- 5432:5432
After starting compose services docker-compose up, I can see my postgres container running (docker ps).
I can also connect to it directly via docker exec
docker exec -it <container_id> psql -d some_db -U some_user
Everything working as expected so far...
However, when I try to connect to my Docker postgres instance via my local psql client:
psql -h localhost -p 5432 -U some_user -d some_db
It is not connecting to the Docker instance of postgres and rather is trying to use my local postgres instance.
Now the strange part...
When I change the port binding in docker-compose.yml
from 5432:5432
to 5433:5432 (5433 can be any open port)
I am able to connect to the Docker postgres instance as expected via my local psql client:
psql -h localhost -p 5433 -U some_user -d some_db
# After being prompted for my password I'm in!
I don't mind binding to a different port, but I'm still so curious what's happening here!
Anyone know what is happening?
Am I just not able to bind to port 5432?
Is this a Docker for Mac thing?
Is there an issue in the way I installed Postgres?
Thanks!
Error:
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: password authentication failed for user "username"
Hello, I am trying to run a program locally using Docker and am getting the error in the title, even though it was working before.
I've tried reinstalling Docker, re-cloning the repo, reinstalling PostgresSQL (the problem started when I installed it for the first time). From reading similar questions, I ensured that the password matches. The password is 'password' for the Docker Postgres Database and I've tried changing it but it still hasn't worked.
I'm using 'docker-compose up -d' and then running tests but I get the error in the title. I've tried running 'docker-compose down' and then redoing it, but I still get the error.
.env file:
FLASK_ENV=development
REDIS_URL=redis://localhost:6379
DATABASE_URL=postgresql+psycopg2://username:password#localhost:5432/programname
PROGRAM_API_APP_NAME=test-prod.compute.random.com
docker-compose.yml:
version: '3'
services:
postgresql:
image: postgres:10-alpine
environment:
- POSTGRES_DB=programname
- POSTGRES_PASSWORD=password
- POSTGRES_USER=username
ports:
- "5432"
redis:
image: redis:5.0.3
ports:
- "6379:6379"
(I don't have the port as 5432:5432 because it didn't work with that and I found an answer to remove the second 5432.)
Boss helped me with this. Apparently, when I installed Postgres, it created a postgres user that had a process that was using port 5432 and even when we killed it, it automatically restarted. To solve this specific problem, we changed the docker-compose file to use port 5433 locally and 5432 in the container. Still have to find out how to get rid of the postgres user.
I am not able to set Password for Postgres using Docker-compose. Postgres is loading without password and with the default user name "postgres", non of the environment variables below seems to applied. below is the db service of my docker-compose.yml file: (version 3)
db:
image: postgres
container_name: postgres
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: db
restart: unless-stopped
volumes:
- ./postgres-data:/var/lib/postgresql/data
ports:
- "5432:5432"
Note I tried using the "-POSTGRES_USER=" as well, it didn't work
Also, I deleted all old containers/volumes.
Any idea?
The problem should be with the volume attached. When your container start it will add the credentials you give him, but then the volume will be attached and that will cause this information being rewritten.
For more information have a look at https://github.com/docker-library/postgres/issues/203#issuecomment-255200501.
The main reason being use of ':' instead of "=" in the environment section.
Ideally it should look like this:
db:
image: postgres
container_name: postgres
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD= pass
- POSTGRES_DB= db
restart: unless-stopped
volumes:
- ./postgres-data:/var/lib/postgresql/data
ports:
- "5432:5432"
Your configuration works fine for me. I suspect you are not using the complete set of correct credentials, which includes the username, password, and database name. If I take your example docker-compose.yaml and run it without modifications, I can connect to the database db like this with username user and password pass:
$ psql -h localhost -U user db
Password for user user:
psql (9.5.7, server 9.6.1)
WARNING: psql major version 9.5, server major version 9.6.
Some psql features might not work.
Type "help" for help.
db=#
Had the same issue.
Couldn't solve it for 2 weeks.
Read almost everything related to it.
And after I finished all PosgreSQL server related processes on local machine, everything goes well.
Start postgres instance:-
docker run --name postgres-0 -e POSTGRES_PASSWORD=mypassword -p 5433:5433 -d postgres
Now we can check docker all running container by this command:-
docker ps