Learn R Programming

taskqueue (version 0.2.0)

db_connect: Connect to PostgreSQL Database

Description

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.

See Also

db_disconnect, taskqueue_options

Examples

Run this code
if (FALSE) {
# Not run:
# Create a new connection
con <- db_connect()

# Reuse existing connection
con2 <- db_connect(con)

# Always disconnect when done
db_disconnect(con)
}

Run the code above in your browser using DataLab