简体   繁体   中英

Trying to connect C# ASP.NET MVC app to a docker container running postgres and I keep getting "password authentication failed"

I have an ASP.NET Identity app that uses a postgres server on the backend. I started the postgres server like this docker run --name postgres-newtester -p 5432:5432 -e POSTGRES_PASSWORD=postgresNewtesterPW1 -d postgres .

Then I went into the cli of the running docker container and ran the following commands:

psql postgres postgres  
CREATE DATABASE amaranth_tests;
CREATE USER amaranth_test_user WITH ENCRYPTED PASSWORD 'amaranth_test_userPW1';
GRANT ALL PRIVILEGES ON DATABASE amaranth_tests TO amaranth_test_user;
\q
exit

Then I changed my connection string in appsettings.Development.json to:

"ConnectionStrings": {
    "db": "Host=localhost;Port=5432;Database=amaranth_tests;Username=amaranth_test_user;Password=amaranth_test_userPW1"
  }

I went to my C# MVC app and ran dotnet restore , then dotnet ef database update . I got the following error in my terminal:

password authentication failed for user "amaranth_test_user"

I'm assuming that this is because my connection string is giving the password for the postgres user but not for the actual docker container. When I switch Password=amaranth_test_userPW1 to Password=postgresNewtesterPW1 I get the same error. So it seems that I need to give both the docker container password and postgres password. How do I put both of these into the connection string? Or is there another way to do this?

PS One follow up question, is there an easier way to do all of this?

I found the problem it's this: https://github.com/sameersbn/docker-postgresql/issues/112#issuecomment-579712540 .

Basically Windows runs PostgreSQL server through localhost on port 5432 so it conflicts with the Docker container postgres which also runs on port 5432 . It runs great on my Mac but on my Windows computer it doesn't work. In the Github issue eddex says to turn off the Windows PostgreSQL service and it will work. He also details a few other options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM