diff --git a/src/NzbDrone.Core/Datastore/DbFactory.cs b/src/NzbDrone.Core/Datastore/DbFactory.cs index 1b21589a2..bd2097d1a 100644 --- a/src/NzbDrone.Core/Datastore/DbFactory.cs +++ b/src/NzbDrone.Core/Datastore/DbFactory.cs @@ -1,6 +1,7 @@ using System; using System.Data.Common; using System.Data.SQLite; +using System.Net.Sockets; using NLog; using Npgsql; using NzbDrone.Common.Disk; @@ -124,6 +125,37 @@ namespace NzbDrone.Core.Datastore throw new CorruptDatabaseException("Database file: {0} is corrupt, restore from backup if available. See: https://wiki.servarr.com/lidarr/faq#i-am-getting-an-error-database-disk-image-is-malformed", e, fileName); } + catch (NpgsqlException e) + { + if (e.InnerException is SocketException) + { + var retryCount = 3; + + while (true) + { + Logger.Error(e, "Failure to connect to Postgres DB, {0} retries remaining", retryCount); + + try + { + _migrationController.Migrate(connectionString, migrationContext); + } + catch (Exception ex) + { + if (--retryCount > 0) + { + System.Threading.Thread.Sleep(5000); + continue; + } + + throw new LidarrStartupException(ex, "Error creating main database"); + } + } + } + else + { + throw new LidarrStartupException(e, "Error creating main database"); + } + } catch (Exception e) { throw new LidarrStartupException(e, "Error creating main database");