framework docs
Database

db_connect()

Get a database connection

Gets a database connection based on the connection name in config.yml. For most use cases, prefer db_query() or db_execute() which handle connection lifecycle automatically.

Usage

db_connect(name)

Arguments

name
Character. Name of the connection in config.yml (e.g., "postgres")

Value

A database connection object (DBIConnection)

Examples

if (FALSE) {
# Preferred: use db_query() which auto-disconnects
users <- db_query("SELECT * FROM users", "postgres")
# Manual connection management (remember to disconnect!)
conn <- db_connect("postgres")
DBI::dbListTables(conn)
DBI::dbDisconnect(conn)
}