Repository navigation
Expand file tree
/
Copy pathREADME.Rmd
More file actions
122 lines (93 loc) · 4.86 KB
/
Copy pathREADME.Rmd
File metadata and controls
122 lines (93 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
output: cynkratemplate::readme_document
---
<!-- README.md and index.md are generated from README.Rmd.
Edit that file and render it the usual way: rmarkdown::render(), devtools::build_readme(), or the Knit button.
The cynkratemplate package must be installed; it supplies the output format. -->
# RPostgres
<!-- badges: start -->
[](https://github.com/r-dbi/RPostgres/actions)
[](https://app.codecov.io/gh/r-dbi/RPostgres/tree/main)
[](https://CRAN.R-project.org/package=RPostgres)
<!-- badges: end -->
RPostgres is a DBI-compliant interface to the postgres database.
It's a ground-up rewrite using C++ and [cpp11](https://github.com/r-lib/cpp11).
Compared to RPostgreSQL, it:
* Has full support for parameterized queries via `dbSendQuery()`, and `dbBind()`.
* Automatically cleans up open connections and result sets, ensuring that you don't need to worry about leaking connections or memory.
* Is a little faster, saving ~5 ms per query.
(For reference, it takes around 5ms to retrieve a 1000 x 25 result set from a local database,
so this is decent speed up for smaller queries.)
* A simplified build process that relies on system libpq.
## Goals and non-goals
RPostgres aims to:
* implement the [DBI specification](https://dbi.r-dbi.org/articles/spec.html) for PostgreSQL,
and to keep proving it by running the `DBItest` suite as part of its own tests
* support parameterized queries through `dbSendQuery()` and `dbBind()`,
so that values never have to be pasted into the SQL string
* map PostgreSQL types to the R types that fit them best:
64-bit integers as [`bit64::integer64`](https://bit64.r-lib.org/reference/bit64-package.html) by default, `bytea` via `blob`, and date-time columns with an explicit time zone
* reach the PostgreSQL features that DBI has no generic for,
such as `LISTEN`/`NOTIFY` through [`postgresWaitForNotify()`](https://rpostgres.r-dbi.org/reference/postgresWaitForNotify.html)
and large objects through [`postgresImportLargeObject()`](https://rpostgres.r-dbi.org/reference/postgresImportLargeObject.html)
* serve AWS Redshift clusters through [`Redshift()`](https://rpostgres.r-dbi.org/reference/Redshift.html),
with the behavior of individual methods adjusted where that cluster differs
It is explicitly not trying to:
* define what the database interface looks like:
the generics, their semantics and the specification belong to DBI;
this package supplies the PostgreSQL implementation
* translate R code into SQL: queries are handed to `dbSendQuery()` and friends as SQL strings
* bundle a client library: no `libpq` source ships with the package;
the build links the one the platform provides and downloads a prebuilt binary where that one is missing or unsuitable
## Installation
```R
# Install the latest RPostgres release from CRAN:
install.packages("RPostgres")
# Or the development version from GitHub:
# install.packages("pak")
pak::pak("r-dbi/RPostgres")
```
The website [Databases using R](https://solutions.posit.co/connections/db/) describes the tools and best practices in this ecosystem.
## Basic usage
```R
library(DBI)
# Connect to the default postgres database
con <- dbConnect(RPostgres::Postgres())
dbListTables(con)
dbWriteTable(con, "mtcars", mtcars)
dbListTables(con)
dbListFields(con, "mtcars")
dbReadTable(con, "mtcars")
# You can fetch all results:
res <- dbSendQuery(con, "SELECT * FROM mtcars WHERE cyl = 4")
dbFetch(res)
dbClearResult(res)
# Or a chunk at a time
res <- dbSendQuery(con, "SELECT * FROM mtcars WHERE cyl = 4")
while(!dbHasCompleted(res)){
chunk <- dbFetch(res, n = 5)
print(nrow(chunk))
}
# Clear the result
dbClearResult(res)
# Disconnect from the database
dbDisconnect(con)
```
## Connecting to a specific Postgres instance
```R
library(DBI)
# Connect to a specific postgres database i.e. Heroku
con <- dbConnect(RPostgres::Postgres(),dbname = 'DATABASE_NAME',
host = 'HOST', # i.e. 'ec2-54-83-201-96.compute-1.amazonaws.com'
port = 5432, # or any other port specified by your DBA
user = 'USERNAME',
password = 'PASSWORD')
```
## Design notes
On the C++ side, a connection owns the libpq connection and remembers at most one current result set.
The result object holds the state of its own query.
An R result object holds its own external pointer to the C++ result, separate from the connection's.
Sending a new query while a result set is still open warns, cancels the old query if it has not completed, and invalidates the old result.
---
Please note that the 'RPostgres' project is released with a [Contributor Code of Conduct](https://rpostgres.r-dbi.org/CODE_OF_CONDUCT.html).
By contributing to this project, you agree to abide by its terms.