Connect to a Database
This article is geared toward those who need to connect to an existing database using an ODBC driver. To review other options, such as using a database R package or JDBC, please refer to the Selecting a database interface page. If the intent is to learn with a local and small database, refer to the example in the dplyr page.
odbcrecognizes the installed drivers using
odbcListDrivers(). Here is an example result:
 "Devart ODBC Driver for PostgreSQL"  "MapR Drill ODBC Driver"  "ODBC Driver 13 for SQL Server"  "Simba Athena ODBC Driver"  "Simba ODBC Driver for Google BigQuery"  "SQL Server"
Determine if a DSN is going to be used to connect to the database. This would be typically something that the Database Administrator, or the technical owner of the database, would setup and provide the R developer a name (known as an alias). Use
dbConnect()to open a database connection in this manner:
con <- dbConnect(odbc(), "DSN name")
If no DSN is available, then the connection needs to usually pass the server address, database name, and the credentials to be used. There may be other settings unique to a given database that will also need to be passed. In the following example, the Trusted_Connection setting is unique to a MS SQL connection:
con <- dbConnect(odbc(), Driver = "SQL Server", Server = "localhost\\SQLEXPRESS", Database = "datawarehouse", Trusted_Connection = "True")
For information specific to how to connect to a specific database vendor, visit the Databses page for a list of the database types we have information available for.