PostgreSQL connection management in R is unnecessarily painful:
DBI::dbConnect() calls with 5+ argumentsrpgconn eliminates this friction through three key principles:
Connection strings are validated before attempting to connect. Instead of:
# ❌ Cryptic error deep in DBI
Error: could not connect to serverYou get:
# ✅ Clear, actionable error
Error: Connection string must include a '/{database}' segment,
e.g. 'postgresql://user@host:5432/dbname'Uses the PostgreSQL URI standard that works everywhere:
psycopg2, sqlalchemy)pg, sequelize)pgx, database/sql)psql, pg_dump)One connection string, any environment.
Configuration files live in user-specific directories (~/.config/rpgconn on Linux/Mac, %APPDATA%/rpgconn on Windows), never in your project repository.
init_yamls() # Creates config in safe location
edit_config() # Opens editor - NO manual file hunting
# ✅ Never accidentally commit secretsrpgconn does one thing well: connect to PostgreSQL databases. It doesn’t:
pool)DBI or dbplyr)dbx or custom scripts)Only depends on: - DBI and RPostgres (required for Postgres anyway) - yaml, fs, stringr, usethis (ubiquitous, stable packages)
No heavy frameworks. No version conflicts.
Simple use cases stay simple:
# Beginner: Just works
Sys.setenv(RPG_CONN_STRING = "postgresql://user:pass@host/db")
cn <- dbc()Complex scenarios are supported without cluttering the simple path:
# Advanced: Custom config, SSL, connection pooling options
cn <- dbc(
cfg = "prod",
cfg_path = "config/prod-db.yml",
opt_path = "config/prod-opts.yml"
)The package supports three PostgreSQL connection string formats:
URI Format (recommended)
postgresql://user:pass@host:5432/db?sslmode=requireKeyword/Value (semicolon)
user=alice;password=secret;host=localhost;port=5432;dbname=mydbKeyword/Value (whitespace) - libpq standard
host=localhost user=alice dbname=mydb port=5432Old approach (DBI direct):
Parse → Connect → (Fail deep in driver)
rpgconn approach:
Validate → Parse → Connect
↓
(Fail fast with clear message)
Validation catches: - Missing database name - Empty user/host - Invalid port numbers - Whitespace (copy/paste errors) - Wrong URI scheme (mysql://, http://)
Handles special characters in passwords and parameters:
# Password contains @ and :
# URL-encoded: p@ss:word → p%40ss%3Aword
"postgresql://user:p%40ss%3Aword@host/db"IPv6 hosts:
"postgresql://user@[2001:db8::1]:5432/db"library(DBI)
library(RPostgres)
# Scattered connection details
host <- "db.example.com"
port <- 5432
dbname <- "mydb"
user <- Sys.getenv("DB_USER")
password <- Sys.getenv("DB_PASSWORD")
# Verbose boilerplate (every script)
cn <- dbConnect(
Postgres(),
host = host,
port = port,
dbname = dbname,
user = user,
password = password,
sslmode = "require",
connect_timeout = 10
)
# ... later ...
dbDisconnect(cn)library(rpgconn)
# One environment variable (set once, use everywhere)
Sys.setenv(RPG_CONN_STRING =
"postgresql://user:[email protected]:5432/mydb?sslmode=require")
# Single function call
cn <- dbc()
# ... later ...
dbd(cn)Lines of code: 16 → 4 (75% reduction) Failure points: Many → Few (validated upfront) Secrets in code: Yes → No (environment variable)
Use DBI::dbConnect() directly when:
rpgconn is a convenience wrapper, not a replacement for understanding DBI.
vignette("quickstart") for 5-minute setupvignette("advanced-workflow") for: