Skip to content

Using RStudio Professional Drivers#

Overview#

Posit's Professional Drivers can be utilized from R and Python.

R#

Install the odbc package#

Use the odbc R package to access your database with R:

  • From an R prompt, install the odbc package (installing odbc will also install DBI, the database interface for R):

    R Console
    install.packages("odbc")
    
Dependencies

On Linux, the odbc R package requires the following system dependencies in Linux:

  • C++11
  • GCC 4.8 or later

On older versions of RHEL, you can install GCC 4.8 with RedHat Developer Toolset:

Terminal
sudo yum install devtoolset-4-gcc-c++

Test connectivity#

  • Test your connection in R with odbc::odbc().

    • If you are using RStudio (the IDE), you can use the New Connection dialog to help you write the connection string, for example:

      R Console
      con <- DBI::dbConnect(odbc::odbc(),
              Driver = "Driver Name",
              Database = "Database",
              UID = "User",
              PWD = "Password",
              Server = "Server",
              Port = 5432)
      
    • If you have a predefined DSN as shown in the Pro Drivers Installation section, you can connect using a DSN name, for example:

      R Console
      con <- DBI::dbConnect(odbc::odbc(), "DSN Name Here")
      

Python#

Install the pyodbc package#

Use the pyodbc Python package to access your database with Python.

  • From the terminal, install the pyodbc package:

    Terminal
    pip install pyodbc
    
Dependencies

Per the pyodbc installation documentation,

Starting with pyodbc 4.0.35, Linux wheels are available from PyPI. However, they do not include their own copies of the unixODBC library files (because that caused problems), so if pip installs pyodbc from those wheel files then unixODBC must be installed separately. Ensure you have unixodbc installed on the server.

Test connectivity#

  • Test your connection in Python with pyodbc.connect

    • If you are using credentials to connect to a database, include them either as a connection string or as keyword arguments, for example:

      Python Interpreter
      import pyodbc
      con=pyodbc.connect(driver='{Driver Name}',
                         database='Database',
                         uid='User',
                         pwd='Password',
                         server='Server',
                         port=5432)
      
    • If you have a predefined DSN as shown in the Pro Drivers Installation section, you can connect using a DSN name, for example:

      Python Interpreter
      import pyodbc
      con=pyodbc.connect('DSN=DSN Name Here')
      

Additional Resources#

Visit our Solutions site for best practices, examples, and additional configurations to use when working with databases and the Posit Professional Drivers.

Troubleshooting#

Refer to Troubleshooting Posit Professional Drivers for additional information on troubleshooting the Connections pane in RStudio (the IDE).