DataContext.Database.EnsureDeleted() results in "terminating connection due to administrator command"
See original GitHub issueI get this error using DatabaseFacade.EnsureDeleted().
Pooling=false in the connection string has no effect.
Npgsql.PostgresException (0x80004005): 57P01: terminating connection due to administrator command
at Npgsql.NpgsqlConnector.<ReadMessage>g__ReadMessageLong|194_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
at Npgsql.NpgsqlDataReader.NextResult()
at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Delete()
at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureDeleted()
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureDeleted()
at <StartupCode$FSI_0006>.$FSI_0006.main@()
Exception data:
Severity: FATAL
SqlState: 57P01
MessageText: terminating connection due to administrator command
File: postgres.c
Line: 3090
Routine: ProcessInterrupts
From discussions in npgsql/npgsql, it looks like the problem is that the data context is connected to the database while it’s trying to delete it.
The connection string:
Host=localhost;Database=postgres;Port=5307;User ID=postgres;Password=dev;Include Error Detail=true;Pooling=false
I was able to work around it by doing raw queries to create a temp database if none exists, connecting to the db via that one, and then running drop database postgres;
.
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yep, that was it. I changed the name of the database, and the problem is no more.
I’d just been following some documentation somewhere that was using the name
postgres
, and since I’m just running it locally, I didn’t have much of a reason to change it.Thanks for taking the time to investigate!
tl;dr for anyone coming across this problem: Don’t call your database
postgres
.Yeah, that all makes sense now.
postgres
is indeed a “special” name, in that it’s a database that’s always expected to exist, precisely in order to perform various administrative tasks such as creating other databases.