interface
SQL.PostgresOrMySQLOptions
interface PostgresOrMySQLOptions
- adapter?: 'postgres' | 'mysql' | 'mariadb'
Database adapter/driver to use. Takes priority over the protocol of the URL.
"mariadb"uses the MySQL driver and reads theMARIADB_*environment variables instead ofMYSQL_*. - allowPublicKeyRetrieval?: boolean
MySQL only. Allow the client to request the server's RSA public key during
caching_sha2_password/sha256_passwordauthentication when the connection is not protected by TLS. Disabled by default because a network attacker can substitute their own key and recover the plaintext password. Enable only for trusted local connections, or use TLS instead. Not needed over TLS, where Bun sends the password directly.caching_sha2_passworddoes not need it over a unix socket either.sha256_passworddoes. - bigint?: boolean
Return large integers as
BigInt. By default they are returned as strings.PostgreSQL: every
int8, whatever its value. MySQL:BIGINTvalues outside the 32-bit range, and alastInsertRowidaboveNumber.MAX_SAFE_INTEGER. - channelBinding?: 'require' | 'disable' | 'prefer'
PostgreSQL only. Whether a SCRAM login over TLS is tied to the server's certificate (SCRAM-SHA-256-PLUS), so that someone who terminates TLS between the client and the server cannot pass the login on.
"prefer": binds the login when the connection uses TLS and the server offers it"require": only accepts a bound login. Otherwise the connection fails withERR_POSTGRES_CHANNEL_BINDING_REQUIRED: without TLS, and when the server asks for an MD5 password, a clear text password, or none."disable": never binds the login
Also the
?channel_binding=parameter of the URL, and thePGCHANNELBINDINGenvironment variable.On MySQL and MariaDB,
"require"throws when the client is created, and the other values have no effect. - connection?: Record<string, string | number | boolean>
PostgreSQL only. Runtime configuration parameters that Bun sends when it opens a connection, for example
{ application_name: "api", search_path: "app" }. - connectionTimeout?: number
Maximum time in seconds to wait when establishing a connection. Does not limit how long a query runs.
- database?: string
Name of the database to connect to.
PGDATABASE,MYSQL_DATABASEandMARIADB_DATABASEtake priority over the database in a URL. - foundRows?: boolean
MySQL and MariaDB only. Report the rows that the
WHEREclause of anUPDATEmatched inaffectedRows, asmysql2, themariadbconnector, PostgreSQL and SQLite do. Without it, the server counts only the rows whose values changed, so anUPDATEthat sets a row to the values it already has reports0. Also the?foundRows=parameter of the URL. - hostname?: string
Database server hostname. For PostgreSQL, a value that starts with
/is the directory of a unix socket. - idleTimeout?: number
Maximum time in seconds a connection can sit idle before it is closed. A connection that runs a query, is reserved, or has an open transaction is not idle.
- maxLifetime?: number
Maximum lifetime in seconds of a connection. A connection that is busy at that moment finishes its queries, its transaction or its reservation first. It takes no new queries, and then closes.
- onclose?: (err: null | Error) => void
PostgreSQL and MySQL: called once for each connection that closes or fails to connect, with the
Errorthat says why. It is nevernull: after SQL.close the code isERR_POSTGRES_CONNECTION_CLOSEDorERR_MYSQL_CONNECTION_CLOSED. A failed attempt passes its own error, for exampleERR_POSTGRES_CONNECTION_REFUSED. - path?: string
Unix domain socket to connect through instead of TCP to
hostname:port. For PostgreSQL, a directory resolves to<path>/.s.PGSQL.<port>. A path that does not exist fails the connection. Also the?path=parameter of the URL. - prepare?: boolean
PostgreSQL only. Use named prepared statements. Each connection keeps the 1,024 it used last.
falsethrows for MySQL and does nothing for MariaDB. - socket?: string
Unix domain socket to connect through (alias for path). Also the
?socket=parameter of the URL. - tls?: boolean | 'allow' | BunFile | 'require' | 'disable' | TLSOptions | 'prefer' | 'verify-ca' | 'verify-full'
Whether to use TLS/SSL for the connection. A string selects the SSL mode (
"disable","allow","prefer","require","verify-ca","verify-full"). ABunFileis the CA certificate to verify the server against, the same as{ ca: Bun.file(path) }, and selects"verify-full".trueand an options object ask for at least"require". An object withca,caFile,checkServerIdentityorrejectUnauthorized: trueraises a mode below"verify-ca"to"verify-full". WithoutrejectUnauthorized,NODE_TLS_REJECT_UNAUTHORIZED=0turns certificate verification off in every mode. PostgreSQL ignores this option on a unix socket.A URL that Bun reads from a
TLS_*environment variable uses"require"unless the URL or this option names a mode.const sql = new SQL(url, { tls: Bun.file("./rds-ca-bundle.pem") }); - transform?: Transform | NameTransform
Renames columns between the database and JavaScript, for example SQL.camel
- url?: string | URL
Connection URL, for example
postgres://user:pass@localhost:5432/mydb.For each setting, an option takes priority over the URL, and the URL over the environment. Without
urland without a connection string, Bun reads a URL from the environment, such asDATABASE_URL, and the other options change single fields of it.With
adapter, aDATABASE_URLthat names another adapter's server is not used, except for the TLS mode it asks for when no other URL applies.