Can my EF application connect to PostgreSQL using user postgres? - postgresql

I am writing an EF application based on https://learn.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?

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.

Related

Access postgres db on host machine from container built with docker compose

I have a Postgres db set up on a ubuntu box and on the same box I have some containers from a docker compose which trying to access the database on this box. (The Postgres/database is NOT in a container. it is installed on the box)
I can access the db fine using dbeaver and can even connect to it from the site running on my dev environment on my pc
I'm using a .net core backend to connect to the db.
I have added this to the end of my backend service in the docker compose:
extra_hosts:
- "host.docker.internal:host-gateway"
Also I have tried adjusting the connection string and nothing works
Server=host.docker.internal;Port=5432;Database=mydb;User Id=xx;Password=xx;
Server=<IP address>;Port=5432;Database=mydb;User Id=xx;Password=xx;
Server=172.17.0.1;Port=5432;Database=mydb;User Id=xx;Password=xx;
Any ideas what to do?
in the logs I can see the following error
Connection id "0HMMVM72IIA14", Request id "0HMMVM72IIA14:00000002": An unhandled exception was thrown by the application.
System.InvalidOperationException: An exception has been raised that is likely due to a transient failure.
---> Npgsql.NpgsqlException (0x80004005): Failed to connect to <IP REMOVED>:5432
---> System.TimeoutException: Timeout during connection attempt
at Npgsql.Internal.NpgsqlConnector.ConnectAsync(NpgsqlTimeout timeout, CancellationToken cancellationToken)
at Npgsql.Internal.NpgsqlConnector.RawOpen(SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)

Postegres ef-core update database produce "42P01: relation "__EFMigrationsHistory" does not exist" error

Setup
Asp.NET core website 2.2 and EF Core 2.2
Postgresql database with multiple schemas and one of the schema already has __EFMigrationsHistory table
when trying
Add-Migration x1 -Context YodaContext
it works
but when trying the following statement
Update-Database -Context YodaContext for the first time (I do not have any tables in this schema this is the first update-database) I am seeing the following error.
Failed executing DbCommand (85ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT "MigrationId", "ProductVersion"
FROM "__EFMigrationsHistory"
ORDER BY "MigrationId";
Npgsql.PostgresException (0x80004005): 42P01: relation "__EFMigrationsHistory" does not exist
at Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext() in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:line 1032
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming) in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 444
at Npgsql.NpgsqlDataReader.NextResult() in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 332
at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1218
at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1130
at System.Data.Common.DbCommand.ExecuteReader()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.GetAppliedMigrations()
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
42P01: relation "__EFMigrationsHistory" does not exist
What I did
I searched this error and I found this bug on github
the suggested solution there was to create the table manually, and I did that, but this did not solve the problem.
I also tried to open a new .NET 5 project and install the latest version of the Npgsql.EntityFrameworkCore.PostgreSQL v 5.0.0 provider to connect to that database and I still facing this problem
this question does not solve the problem EF Core - Table '*.__EFMigrationsHistory' doesn't exist
I had same problem because my database (POSTGRESQL) had another schema that contains ____EFMigrationsHistory table. I think it is a provider's bug, in my case postgresql provider. I created same table manually on new schema then update database command executed successfully
I faced this problem while working with a very huge enterprise, so the solution applied by the enterprise themselves, they deleted the schema and created it again (as they told me) maybe they did some other stuff, I do not know.
After that everything went well, and the Update-Database statement worked perfectly well.
I also have the same issue, but I don't have permissions to delete schema and create it again in my company.
I solved problem in a another way:
I run command to execute migration. Add-Migration InitialIdentityUser -Context IdentityUserDbContext
After that, I use tricky command Script-Migration -from 0. This command generate bunch of sql queries to create schema and tables.
I use pretty cool tool - flyway for my migrations https://flywaydb.org/
Execute baseline command with flyway tool.
flyway baseline -locations=filesystem:. -url=... -user=... -password=... -baselineVersion="001" -schemas=...
Finally, execute migrate command to migrate all sql tables.
flyway migrate -locations=filesystem:. -url=... -user=... -password=... -schemas=...

postgresql pg_database_size throwing exception on random times

We are using Azure database for PostgreSQL ( Service ) for creating DB for each user when user register to the application ( less than 25 users databases right now ).
For reporting purpose we need information which each user's DB size.
To retrieve database size we have a Postgres function which fires the following query
SELECT pg_database.datname , pg_database_size(pg_database.datname) FROM
pg_database
We execute this function every hour throw azure function but at random time Postgres throw exceptions
Exception: Npgsql.PostgresException (0x80004005): 58P01: could not read directory "base/16452": No such file or directory at...
Exception remain same at most of the time with different directory or file location
Sometimes it also throws the exception
Exception: Npgsql.NpgsqlException (0x80004005): Exception while reading from stream ---> System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ---> System.Net.Sockets.SocketException
Working on the solution at the MSDN forums here.

Failed connection in MongoVUE

I'm trying to create a new connection in MongoVUE but I have an error : "Connection was refused".
The connection configuration is:
Name: database
Server: 127.0.0.1
Port: 27017(default)
Username:admin
Connection was refused Unable to connect to server 127.0.0.1:27017:
Object reference not set to an instance of an object.. Type:
MongoDB.Driver.MongoConnectionException Stack: at
MongoDB.Driver.Internal.DirectMongoServerProxy.Connect(TimeSpan
timeout, ReadPreference readPreference) at
MongoDB.Driver.MongoServer.Connect(TimeSpan timeout) at
MongoDB.Driver.MongoServer.Connect() at
MangoUI.MMongo.QSDlL5xzK686iCExThO(Object ) at
MangoUI.MMongo.Open(Boolean mustWrite) at MangoUI.MMongo.Open()
at MangoUI.MConnection.get_IsValid() at
MangoUI.WinConnect.SCOjR9kYRPerNNngykW(Object ) at
MangoUI.WinConnect.btnTest_Click(Object sender, EventArgs e)
Object reference not set to an instance of an object. Type:
System.NullReferenceException Stack: at
MongoDB.Driver.MongoServerInstance.RefreshStateAsSoonAsPossible()
at MongoDB.Driver.Internal.MongoConnection.HandleException(Exception
ex) at
MongoDB.Driver.Internal.MongoConnection.SendMessage(BsonBuffer buffer,
Int32 requestId) at
MongoDB.Driver.Internal.MongoConnection.SendMessage(MongoRequestMessage
message) at
MongoDB.Driver.Operations.CommandOperation`1.Execute(MongoConnection
connection) at
MongoDB.Driver.MongoServerInstance.RunCommandAs[TCommandResult](MongoConnection
connection, String databaseName, IMongoCommand command) at
MongoDB.Driver.MongoServerInstance.Ping(MongoConnection connection)
at MongoDB.Driver.MongoServerInstance.Connect() at
MongoDB.Driver.Internal.DirectMongoServerProxy.Connect(TimeSpan
timeout, ReadPreference readPreference)
How can I fix it and create connection? Help please.
Maybe you have solved this problem.I just provide how I solve for whom come with it later:
check if your mongodb is on.
Open the browser, input the urllocalhost:27017, you should find that the web page have words like"it seems that you attend to connect the mongodb", then go to step 3.
If not go to step 2.
keep your mongodb on. you should add the install directory to the path variable.
Then create a directory to store you db,for example"D:\data\db\".
Then input this commandmongod --dbpath D:\data\db in the console, never close the console if you want to make connection.
Then go step 3.
Use MongoBooster rather than MongVUE. MongoVUE is nolooger update since 2014. However, mongobooster is well and good,support mongodb from 2.2 to 3.2
That is all.

Installing OfBiz on PostgreSQL

I attempted to setup OFBiz on PostgreSQL, but when I run the server, I get this error when I visit http://server_address:8080/ecommerce/:
org.ofbiz.widget.screen.ScreenRenderException: Error rendering screen [component://ecommerce/widget/CommonScreens.xml#main]: java.lang.IllegalArgumentException: Error running script at location [component://order/webapp/ordermgr/WEB-INF/actions/entry/catalog/Category.groovy]: org.ofbiz.entity.GenericEntityException: org.ofbiz.entity.transaction.GenericTransactionException: The current transaction is marked for rollback, not beginning a new transaction and aborting current operation; the rollbackOnly was caused by: Failure in findByCondition operation for entity [ProdCatalogCategory]: org.ofbiz.entity.GenericEntityException: org.postgresql.Driver (org.postgresql.Driver). Rolling back transaction.org.ofbiz.entity.GenericEntityException: org.postgresql.Driver (org.postgresql.Driver) (org.postgresql.Driver (org.postgresql.Driver)) (The current transaction is marked for rollback, not beginning a new transaction and aborting current operation; the rollbackOnly was caused by: Failure in findByCondition operation for entity [ProdCatalogCategory]: org.ofbiz.entity.GenericEntityException: org.postgresql.Driver (org.postgresql.Driver). Rolling back transaction.org.ofbiz.entity.GenericEntityException: org.postgresql.Driver (org.postgresql.Driver) (org.postgresql.Driver (org.postgresql.Driver))) (Error running script at location [component://order/webapp/ordermgr/WEB-INF/actions/entry/catalog/Category.groovy]: org.ofbiz.entity.GenericEntityException: org.ofbiz.entity.transaction.GenericTransactionException: The current transaction is marked for rollback, not beginning a new transaction and aborting current operation; the rollbackOnly was caused by: Failure in findByCondition operation for entity [ProdCatalogCategory]: org.ofbiz.entity.GenericEntityException: org.postgresql.Driver (org.postgresql.Driver). Rolling back transaction.org.ofbiz.entity.GenericEntityException: org.postgresql.Driver (org.postgresql.Driver) (org.postgresql.Driver (org.postgresql.Driver)) (The current transaction is marked for rollback, not beginning a new transaction and aborting current operation; the rollbackOnly was caused by: Failure in findByCondition operation for entity [ProdCatalogCategory]: org.ofbiz.entity.GenericEntityException: org.postgresql.Driver (org.postgresql.Driver). Rolling back transaction.org.ofbiz.entity.GenericEntityException: org.postgresql.Driver (org.postgresql.Driver) (org.postgresql.Driver (org.postgresql.Driver))))
However, I have no idea what it means. I setup a basic database called ofbiz with the owner as the ofbiz user. Then ran ./ant load-demo to populate the database with the demo data. My operating system is Debian GNU/Linux 7.2. I should also note that I'm a newcomer to PostgreSQL, OFBiz, and Java. I'm sorry about formatting, this is how it came.
Your question is not clear. I am assuming that PostgreSQL driver are missing. By default ofbiz doesnt come with PostgreSQL drivers so you need to download it. Go to ofbiz directory and run the following command
ant download-PG-JDBC in windows or ./ant download-PG-JDBC for linux .