question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataContext.Database.EnsureDeleted() results in "terminating connection due to administrator command"

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
reinuxcommented, Jul 26, 2021

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.

2reactions
rojicommented, Jul 26, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found