open static method

Future<PostgresConnection> open({
  1. String host = 'localhost',
  2. int port = 5432,
  3. required String database,
  4. String? username,
  5. String? password,
  6. bool ssl = false,
  7. int maxConnections = 5,
})

Implementation

static Future<PostgresConnection> open({
  String host = 'localhost',
  int port = 5432,
  required String database,
  String? username,
  String? password,
  bool ssl = false,
  int maxConnections = 5,
}) async {
  final pool = pg.Pool<void>.withEndpoints(
    [
      pg.Endpoint(
        host: host,
        port: port,
        database: database,
        username: username,
        password: password,
      ),
    ],
    settings: pg.PoolSettings(
      maxConnectionCount: maxConnections,
      sslMode: ssl ? pg.SslMode.require : pg.SslMode.disable,
    ),
  );
  try {
    await pool.execute('select 1');
    return PostgresConnection._(pool);
  } catch (e) {
    await pool.close(force: true);
    throw ConnectionException(
      'Cannot connect to PostgreSQL at $host:$port/$database: $e',
      cause: e,
    );
  }
}