download_wrds.get_wrds_connection()

Establish a connection to Wharton Research Data Services (WRDS).

Usage

Source

download_wrds.get_wrds_connection()

Opens a SQLAlchemy connection to the WRDS PostgreSQL database. Credentials are read from the ‘WRDS_USER’ and ‘WRDS_PASSWORD’ environment variables, which can also be supplied through a ‘.env’ file in the working directory (‘load_dotenv’ is called before the lookup). The connection uses the ‘postgresql+psycopg2’ driver, requires SSL, and targets ‘wrds-pgdata.wharton.upenn.edu’ on port 9737. Pool pre-ping is enabled so stale connections are detected and recycled automatically.

Returns

sqlalchemy.engine.Connection
An open SQLAlchemy connection to the WRDS database. Pass it to any ’download_data_wrds_*’ helper or use it directly with ‘polars.read_database’. Close it via ‘disconnect_connection’ when done.

Raises

ValueError

If ‘WRDS_USER’ or ‘WRDS_PASSWORD’ is not set in the environment or ‘.env’ file. The ‘load_wrds_credentials’ helper performs this check before the connection is opened.

sqlalchemy.exc.OperationalError
If the database host is unreachable or the credentials are rejected.

See Also

set_wrds_credentials()

Interactively prompts for credentials and writes them to a ‘.env’ file in the project or home directory.

disconnect_connection()

Closes the connection returned by this function.

Examples

import os
os.environ['WRDS_USER'] = 'your_username'
os.environ['WRDS_PASSWORD'] = 'your_password'
from tidyfinance import get_wrds_connection, disconnect_connection
con = get_wrds_connection()
# ... query WRDS via con ...
disconnect_connection(con)