I have read-only access to a Postgres database. I can not write to the database.
Q. Is there a way to construct and run a SQL query where I join a data frame (or other R object) to a table in a read-only Postgres database?
This is for accessing data from WRDS, https://wrds-www.wharton.upenn.edu/
Here’s an attempt at pseudocode
#establish a connection to a database
con <- dbConnect( Postgres(),
host = 'host.org',
port = 1234,
dbname = 'db_name',
sslmode = 'require',
user = 'username', password = 'password')
#create an R dataframe (or other object)
df <- data.frame( customer_id = c('a123', 'a-345', 'b0') )
#write a sql query we will run
sql_query <- "
SELECT t.customer_id, t.* FROM df t
LEFT JOIN table_name df
on t.customer_id = df.customer_id
"
my_query_results <- dbSendQuery(con, sql_query)
temp <- dbFetch(res, n = 1)
dbClearResult(res)
my_query_results
Note and edit: The example query I provided is intentionally super simple for example purposes.
In my actual queries, there might be 3 or more columns I want to join on, and millions of rows I want to join on.
2
Answers
If your join is on a single condition, it can be rewritten using an
in
clause:In SQL:
Programmatically from R:
Use the
copy_inline
function from thedbplyr
package, which was added following an issue filed on this topic. See also the question here.An example of its use is found here and also below.
Before running the code below, run code like this in your R console:
Then (assuming you have the
dplyr
,dbplyr
, andRPostgres
packages installed) you should be able to copy-paste the following code to check that it works for you. Many more examples can be found here.Created on 2023-04-23 with reprex v2.0.2