Problem with PostgresSQL password authentication failed for user when trying to access or fetch data from database?

0

I have an issue when trying to fetch data from postgressql. I'm playing with microservices arhitecture using .NET Core 5.0 and postgressql as my db solution.

I'm trying to containerize my app by using docker containers. And everything works fine till I try to fetch some data through API.

I got this type of error:

Npgsql.PostgresException (0x80004005): 28P01: password authentication failed for user "admin"
   at Npgsql.NpgsqlConnector.<ReadMessage>g__ReadMessageLong|194_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
   at Npgsql.NpgsqlConnector.AuthenticateMD5(String username, Byte[] salt, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnector.Authenticate(String username, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.ConnectorPool.OpenNewConnector(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.ConnectorPool.<>c__DisplayClass38_0.<<Rent>g__RentAsync|0>d.MoveNext()
--- End of stack trace from previous location ---
   at Npgsql.NpgsqlConnection.<>c__DisplayClass41_0.<<Open>g__OpenAsync|0>d.MoveNext()
--- End of stack trace from previous location ---
   at Dapper.SqlMapper.QueryRowAsync[T](IDbConnection cnn, Row row, Type effectiveType, CommandDefinition command) in /_/Dapper/SqlMapper.Async.cs:line 472

From the backend prespective, situation is pretty simple where I'm trying to fetch data by productName:

public async Task<Coupon> GetDiscount(string productName)
    {
        using var connection = new NpgsqlConnection
            (_configuration.GetValue<string>("DatabaseSettings:ConnectionString"));

        var coupon = await connection.QueryFirstOrDefaultAsync<Coupon>
            ("SELECT * FROM Coupon WHERE ProductName = @ProductName", new { ProductName = productName });

        if (coupon == null)
            return new Coupon 
            { ProductName = "No Discount", Amount = 0, Description = "No Discount Desc" };

        return coupon;
    }

My appsettings.json file, or to be more precise connection string towards postgressql is this:

 "DatabaseSettings": {
    "ConnectionString": "Server=localhost;Port=5432;Database=DiscountDb;User Id=admin;Password=admin1234;"
  },

In docker compose override yml file I have:

discountdb:
    container_name: discountdb
    environment:
      - POSTGRES_USER=admin
      - POSTGRES_PASSWORD=admin1234
      - POSTGRES_DB=DiscountDb
    restart: always
    ports:
        - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data/

And as I can see, in my portainer I can see that discountdb is up and running enter image description here

I'm missing something and I don't know what.

postgresql
docker
.net-core
asked on Stack Overflow Apr 15, 2021 by Svinjica

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0