Function connectDatabase

  • Connect to a PostgreSQL server.

    Opens PostgreSQL alone, in every runtime: there is no fallback to a local database, so a server that cannot be reached fails the call. Any connection string pg reads is accepted (a postgres:// or postgresql:// URL, a socket: URL, a socket path).

    Parameters

    Returns Promise<StorageAdapter>

    Error when the configuration names no server: no pool, an empty or unset connection string, and no host, database or user, as connectDatabase(process.env.DATABASE_URL) gives with DATABASE_URL unset. Nothing is opened then; the call does not read DATABASE_URL on its own.

    // Simple connection string
    const db = await connectDatabase('postgresql://user:pass@host/db');

    // With full config
    const db = await connectDatabase({
    host: 'db.example.com',
    database: 'myapp',
    user: 'dbuser',
    password: process.env.DB_PASSWORD,
    ssl: true
    });