Skip to contents

Establishes a connection to the Wharton Research Data Services (WRDS) database using the RPostgres package. It requires that the RPostgres package be installed and that valid WRDS credentials be set as environment variables.

Usage

get_wrds_connection(quiet = FALSE)

Arguments

quiet

A logical indicating whether to suppress the messages about the WRDS connection and the download progress. Defaults to FALSE.

Value

An object of class DBIConnection representing the connection to the WRDS database. This object can be used with other DBI-compliant functions to interact with the database.

Details

The function checks if the RPostgres package is installed before attempting to establish a connection. It uses the host, dbname, port, and sslmode as fixed parameters for the connection. Users must set their WRDS username and password as environment variables WRDS_USER and WRDS_PASSWORD, respectively, before using this function.

WRDS account setup

A WRDS username and password alone are not enough to connect. Before the first connection:

  1. Get a WRDS account through an institution that subscribes to the data you need (e.g., CRSP and Compustat).

  2. Enroll in two-factor authentication (Duo) for your WRDS account. WRDS requires it for all connections, including connections from R.

  3. Log in to the WRDS website (https://wrds-www.wharton.upenn.edu) once and accept the terms of use, if WRDS asks you to.

  4. Store your username and password with set_wrds_credentials(), or set the WRDS_USER and WRDS_PASSWORD environment variables, and restart R.

  5. When you connect, approve the Duo push notification on your registered device. The connection waits until you approve it.

If the connection fails, the error message lists these steps. Unless quiet = TRUE, the function says when it starts connecting and when the connection succeeds, so you know when to approve the Duo request and when the download starts.

Examples

if (FALSE) { # \dontrun{
  # Before using this function, set your WRDS credentials:
  # Sys.setenv(WRDS_USER = "your_username", WRDS_PASSWORD = "your_password")

  # con <- get_wrds_connection()
  # Use `con` with DBI-compliant functions to interact with the WRDS database
  # Remember to disconnect after use:
  # disconnect_connection(con)
} # }