Can my EF application connect to PostgreSQL using user postgres?

0

I am writing an EF application based on https://docs.microsoft.com/en-us/ef/core/get-started/?tabs=netcore-cli#create-the-database, except that I am using postgresql.

My DbContext class has

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{                                                                                                                                                        
    optionsBuilder.UseNpgsql(
        @"Server=localhost;Port=5432;Database=Blogging;User Id=postgres;Password=abc;");
}

and I fail to run it:

$ dotnet run
Inserting a new blog
Unhandled exception. Npgsql.PostgresException (0x80004005): 28P01: password authentication failed for user "postgres"
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnector.AuthenticateMD5(String username, Byte[] salt, Boolean async)
   at Npgsql.NpgsqlConnector.Authenticate(String username, NpgsqlTimeout timeout, Boolean async)
   at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.ConnectorPool.AllocateLong(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransaction(IsolationLevel isolationLevel)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.BeginTransaction()
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList`1 entries)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList`1 entriesToSave)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(DbContext _, Boolean acceptAllChangesOnSuccess)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
   at EFGetStarted.Program.Main() in /home/ethan/mydata/academic discipline/study objects/areas/formal systems/math/categories of mathematics/meta math, or foundations of math/composition/set theory/a class of sets/operations/product space/a subset ie relation/programming/database/data models/specific-data-models/convert/add-query-engine-above-query-engine/Relational2Object/CSharp/EntityFramework/official/202010/code/EFGetStarted/Program.cs:line 14
  Exception data:
    Severity: FATAL
    SqlState: 28P01
    MessageText: password authentication failed for user "postgres"
    File: auth.c
    Line: 328
    Routine: auth_failed

When connecting to PostgreSQL using psql instead of my application, I can run sudo -u postgres psql -U postgres and it succeeds, and on the other hand:

$ psql -U postgres 
psql: FATAL:  Peer authentication failed for user "postgres"

So I thought the following would work, but it still fails.

$ sudo -u postgres dotnet run
/usr/share/dotnet/sdk/3.1.100/NuGet.targets(528,5): error : Unable to obtain lock file access on '/tmp/NuGetScratch/lock/5d56c6a196882e7c1183dc543c1302123bf253ba' for operations on '/home/ethan/.nuget/NuGet/NuGet.Config'. This may mean that a different user or administator is holding this lock and that this process does not have permission to access it. If no other process is currently performing an operation on this file it may mean that an earlier NuGet process crashed and left an inaccessible lock file, in this case removing the file '/tmp/NuGetScratch/lock/5d56c6a196882e7c1183dc543c1302123bf253ba' will allow NuGet to continue. 

The build failed. Fix the build errors and run again.

Why does it still fail?

Can my EF application connect to PostgreSQL using user postgres?

Is user postgres by convention to have the most privileges?

Is it a good idea to let an application to access PostgreSQL with user postgres?

postgresql
entity-framework
asked on Stack Overflow Jan 10, 2020 by Ethan • edited Jan 10, 2020 by Ethan

1 Answer

1

There is always a superuser in a PostgreSQL database, and typically it is called postgres.

On no account you may use that user for your application. Doing so is an unnecessary and dangerous security problem. Create another user that is not a superuser.

To debug your login problem, examine pg_hba.conf and the PostgreSQL log file.

answered on Stack Overflow Jan 10, 2020 by Laurenz Albe

User contributions licensed under CC BY-SA 3.0