3 Introduction to database connections
3.1 Connect with the Connections pane
Connect using the features of the RStudio IDE
- The connections pane (top right hand corner of the RStudio IDE) can guide through establishing database connections.

3.2 Connecting via DSN
Connect using defined Data Source Name (DSN). This requires an ODBC driver.
Load the
DBIandodbcpackagesUse
odbcListDatasourcesto list available DSNsUse
dbConnectto connect to a database using theodbcfunction and a DSNDisconnect using
dbDisconnect
3.3 Connect with a connection string
Connect by specifying all connection details in dbConnect
Use
dbConnectandodbcto connect to a database, but this time all connection details are providedDisconnect using
dbDisconnect
3.4 Secure connection details
Use config, keyring, or environment variables to secure connection credentials
Load the
configpackageGet the current config using the
getfunction and store the results in an object calledconfigUse
strto investigate the contents ofconfigConnect using details provided in
configDisconnect using
dbDisconnectLoad the
keyringpackageStore the database username and password using
keyring. The username isrstudio_devand the password isdev_userUse the stored credentials along with
dbConnectto connect to the databaseDiscnonnect using
dbDisconnectThe
.Renvironfile contains entries to create environment variables forPG_USERandPG_PWD. These variables can be read usingSys.getenv().Connect to the database using the credentials stored in
.RenvironandSys.getenv()Disconnect using
dbDisconnectStore connection details using
options()Connect using the credentials accessed via
getOptionDisconnect using
dbDisconnectInteractively prompt users for input using
rstudioapi::askForPassword()Disconnect using
dbDisconnect