Prisma/ User `postgres` was denied access on the database `practice.public` - prisma

I'm quite new to Prisma, so if I overlook something basic, please forgive me.
I was running postgreSQL DB on a docker container and tried npx prisma migrate dev from local.
However, the following error occurred:
Error: P1010: User `postgres` was denied access on the database `practice.public`
my docker-compose.yml file and .env file were the followings:
// docker-compose.yml
version: "3"
services:
postgres:
image: postgres:14.1
container_name: postgres
hostname: postgres
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: practice
volumes:
- ./db:/var/lib/postgresql/data
restart: unless-stopped
// .env
DATABASE_URL="postgresql://postgres:postgres#localhost:5432/practice?schema=public"
I tried everything I could come up with, and accidentally found a solution.
First, I checked the username of my host computer.
echo $USER
>myusername
Then put the username into the DATABASE_URL in .env file, instead of the POSTGRES_USER I defined in docker-compose.yml.
// .env
DATABASE_URL="postgresql://myusername:postgres#localhost:5432/practice?schema=public"
npx prisma dev migrate worked for some reason. Congrats.
But I haven't the least idea of why it worked……
If you shed some lights on it, I would really appreciate it.

Related

After using docker-compose down,i lost my data

First,i changed pg_hba.conf,changed "host all all all scram-sha-256" to "host all all all md5". after that,i executed "docker-compose down and docker-compose up -d",but not executed "docker-compose down -v".
After docker starts successfully,I found that the data is lost.i executed "docker volume ls",the result is empty.what caused this phenomenon?
Could someone possibly give me some pointers?Thank you very much!
This is my docker-compose.yml
db:
image: postgres
container_name: postgres
restart: always
environment:
POSTGRES_DB: postgres
POSTGRES_USER: polardb
POSTGRES_PASSWORD: 123456
ports:
- 5432:5432
volumes:
- /opt/deploy/postgres/data:/var/lib/postgresql/data
What was lost was the data of the test server, and the data should not be recovered, but I wanted to understand what caused it, so I found a new server for testing, and found that the data was not lost after the same steps.

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.

Postgres in docker returns 'password authentication failed (..) roles "username" does not exist'

What I'm trying to do: connect to postgres db with a FastAPI app via common docker-compose file. It used to work until I changed postgres configuration from default.
I'm aware this is a common problem and I tried following every tip I found so far.
I did double check spelling of evn variables. I did remove volumes, images, networks and rebuild it from scratch multiple times by now.
Here are relevant parts of docker-compose
version: "3.4"
networks:
internal:
external: false
services:
db:
image: postgres:11
ports:
- "5432:5432"
environment:
- POSTGRES_USER=test_user
- POSTGRES_PASSWORD=test_pass
- POSTGRES_DB=test_db
networks:
- internal
my_app:
depends_on:
- db
networks:
- internal
Here's how I present db to app in code.
DATABASE_URL = "postgres://test_user:test_pass#db:5432/test_db"
I continue to get 'password authentication failed for user "test_user" / Role "test_user" does not exist. / Connection matched pg_hba.conf line 95: "host all all all md5"
What did I manage to miss?
For those who find it, the problem was actually in how I tried to provide env variables.
environment:
- POSTGRES_USER: test_user
- POSTGRES_PASSWORD: test_pass
- POSTGRES_DB: test_db
works

Connect to Postgres using Docker

I'm kinda lost. I have a postgres database running in a docker container which I started using docker compose. Everything was working fine until recently I get an error saying
FATAL: password authentication failed for user "postgres"
I connected to the database using spring - jdbc.
application.properties
## PostgreSQL
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=password
Dockerfile
FROM library/postgres
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD password
ENV POSTGRES_DB mydb
docker-compose
version: '3'
services:
database:
build: ./postgres
ports:
- "5432:5432"
restart: unless-stopped
I can connect to the database using pgAdmin, however I cant connect using my spring-application or SQuirreL.
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"(pgjdbc: autodetected server-encoding to be ISO-8859-1, if the message is not readable, please check database logs and/or host, port, dbname, user, password, pg_hba.conf)
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:525)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:146)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:211)
at org.postgresql.Driver.makeConnection(Driver.java:459)
at org.postgresql.Driver.connect(Driver.java:261)
at net.sourceforge.squirrel_sql.fw.sql.SQLDriverManager.getConnection(SQLDriverManager.java:147)
at net.sourceforge.squirrel_sql.client.mainframe.action.OpenConnectionCommand.executeConnect(OpenConnectionCommand.java:148)
at net.sourceforge.squirrel_sql.client.mainframe.action.OpenConnectionCommand.lambda$execute$0(OpenConnectionCommand.java:93)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:830)
I could connect to Postgresql Database inside Docker container. I suppose you should write database name inside docker-compose file. Here mine is db inside docker-compose. In addition I wrote all postgres confs inside compose file instead of Dockerfile. Then I wrote jdbc:postgresql://db:5432/abdu like this. Here instead of localhost I wrote db name like in compose file. And also make sure you write database username and password correctly. Hope it helps.
application.properties
spring.datasource.username=postgres
spring.datasource.password=password
spring.datasource.jdbc-url=jdbc:postgresql://db:5432/abdu
Dockerfile
FROM openjdk:8
ADD jarfolder/docker-time-zona.jar docker-time-zona.jar
EXPOSE 8086
ENTRYPOINT ["java", "-jar", "/docker-time-zona.jar"]
ENV TZ=America/Los_Angeles
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
docker-compose.yml
version: '3'
services:
web:
build: .
db:
restart: always
container_name: docker-postgresql
image: postgres:9.6
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=postgres
- POSTGRES_DB=abdu
- PGDATA=/var/lib/postgresql/data/pgdata
ports:
- "5432:5432"
Well I kinda fixed it. I changed the username and the password just because I was desperate. It worked and now I am even more confuse than before

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"