diff --git a/app/lib/database/database.dart b/app/lib/database/database.dart index ff045235b3..9f194de3ea 100644 --- a/app/lib/database/database.dart +++ b/app/lib/database/database.dart @@ -100,14 +100,21 @@ Future<(String, String?)> _startOrUseLocalPostgresInDocker() async { throw StateError('Missing connection URL in Appengine environment.'); } + final socketFile = File('/tmp/pub_dev_postgres/run/.s.PGSQL.5432'); + // the default connection URL for local server final url = Uri( scheme: 'postgresql', - host: 'localhost', - port: 55432, path: 'postgres', userInfo: 'postgres:postgres', - queryParameters: {'sslmode': 'disable'}, + queryParameters: { + // If the socket file is present, let's try to use it, otherwise connect through the port. + // Both has benefits: + // - (during startup) calling the port is a blocking call and waits until the service is started, + // - (while the service is running) the socket file seems to provide a bit faster connection. + 'host': await socketFile.exists() ? socketFile.path : 'localhost:55432', + 'sslmode': 'disable', + }, ).toString(); try { @@ -122,6 +129,9 @@ Future<(String, String?)> _startOrUseLocalPostgresInDocker() async { 'Unexpect exit code from tool/start-local-postgres.sh\n${pr.stderr}', ); } + // The socket file may be present sooner than the server is accepting connections. + // This arbitrary wait is probably enough to have a working server at this point. + await Future.delayed(Duration(seconds: 2)); } return (url, null); } diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 872f47ea51..b1a797ae16 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -49,7 +49,7 @@ dependencies: pana: '0.23.0' # 3rd-party packages with pinned versions mailer: '6.5.0' - postgres: '3.5.8' + postgres: '3.5.9' ulid: '2.0.1' tar: '2.0.0' api_builder: diff --git a/pubspec.lock b/pubspec.lock index 8f5f5bfd5a..02a706360e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -661,10 +661,10 @@ packages: dependency: transitive description: name: postgres - sha256: "96d7f40e079b96cdbfed13d71c64668cfd79c35bc17223c6b3ac0ca63bff72f0" + sha256: fefbbfe749c6130e5096588b9c4459173684c695952cd7636ab19be76f255469 url: "https://pub.dev" source: hosted - version: "3.5.8" + version: "3.5.9" protobuf: dependency: transitive description: