Learn R Programming

rpg (version 1.6)

connect: PostgreSQL connection

Description

Manage database connection

Usage

connect(dbname, ...)

disconnect()

make_service(name)

Arguments

dbname

name of the database or a valid libpq connection string

...

named optional connection parameters

name

the service name

Value

connect returns one of:

CONNECTION_OK Succesful connection
CONNECTION_BAD Connection failed

Details

If no connection parameters are supplied, the connection will fallback to default parameters. Usually this establishes a connection on the localhost to a database, if it exists, with the same name as the user.

Valid keywords and their defaults can be obtained by calling get_conn_defaults(all = TRUE). A valid libpq connection string is composed of keyword = value pairs separated by whitespace. You can either pass the entire string or use named arguments. The names of the arguments will be used as keywords and their values as values.

If a password was required but not provided, connect will will open a dialog and prompt for a password. The connection is then re-tried and the status returned.

disconnect will free any query results as well as clean up the connection data. It is called in the pakcage .Last.lib function when exiting R.

The make_service function will write the current connection settings to a special set of files recognized by PostgreSQL's libpq. After calling this function, you will be able to reconnect using the service name as in connect(service=name). If the connection requires a password, that will be saved as well. This is not likely to work on Windows since the file locations are different between Unix-like and Windows. File a bug with the libpq folks if that upsets you.

Examples

Run this code
# NOT RUN {
fetch("SHOW search_path") # default connection
connect("test")
connect(dbname = "test")
connect(dbname = "test", host = "localhost")
connect("dbname = test host = localhost")
disconnect()
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab