Establishes a connection to the PostgreSQL database using credentials from
environment variables or taskqueue_options(). If a valid connection
is provided, it returns that connection instead of creating a new one.
Usage
db_connect(con = NULL)
Value
A PqConnection object from the RPostgres package that can be used
for database operations.
Arguments
con
An existing database connection object. If provided and valid,
this connection is returned. If NULL (default), a new connection is created.
Details
Connection parameters are read from environment variables set in .Renviron:
PGHOST: Database server hostname
PGPORT: Database server port (typically 5432)
PGUSER: Database username
PGPASSWORD: Database password
PGDATABASE: Database name
The function automatically sets client_min_messages to WARNING to
reduce console output noise.
if (FALSE) {
# Not run:# Create a new connectioncon <- db_connect()
# Reuse existing connectioncon2 <- db_connect(con)
# Always disconnect when donedb_disconnect(con)
}