【发布时间】:2022-02-10 17:20:47
【问题描述】:
我在尝试从 postgressql 获取数据时遇到问题。我正在使用 .NET Core 5.0 和 postgressql 作为我的数据库解决方案来使用微服务架构。
我正在尝试使用 docker 容器将我的应用程序容器化。在我尝试通过 API 获取一些数据之前,一切正常。
我遇到了这种类型的错误:
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
从后端来看,情况很简单,我试图通过 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;
}
我的 appsettings.json 文件,或者更准确地说是与 postgressql 的连接字符串是这样的:
"DatabaseSettings": {
"ConnectionString": "Server=localhost;Port=5432;Database=DiscountDb;User Id=admin;Password=admin1234;"
},
在 docker compose 覆盖 yml 文件中我有:
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/
如我所见,在我的搬运工中,我可以看到 discountdb 已启动并正在运行
我错过了一些东西,我不知道是什么。
【问题讨论】:
-
好吧,要么没有设置密码,要么与您使用的密码不同。
标签: postgresql docker .net-core