pg_con: Create connection object to database on a PostgreSQL server

Description Usage Arguments Details Value Warning Examples

Description

Creates a connection object that can be used to connect to a specific database an a PostgreSQL database server. This function requires that connection credentials including username and password be stored in your .Renviron file. It will not be able to locate your connection credentials otherwise.

Usage

1
pg_con(host_label, dbname, user_label, pw_label, port = "5432")

Arguments

host_label

a string label to identify the server host

dbname

a database on a PostgreSQL server

user_label

a string label to identify the server username

pw_label

a string label to identify the server password

port

a port string used to connect to the database

Details

See the Readme file at https://github.com/arestrom/repg for details on how to set up your .Renviron file. The string labels for host_label, user_label, pw_label are needed to locate your connection credentials in your .Renviron file.

Value

A connection object to a database located on a PostgreSQL server

Warning

Remember to close your connection after you are done. See example below.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## Not run: 
# Create a connection object to a PostgreSQL database on localhost
db_con_local = pg_con(
  host_label = "my_pg_host_local",
  dbname = "sport_sampling",
  user_label = "my_pg_username_local",
  pw_label = "my_pg_password_local",
  port = "5432")

# Connect to database and retrive a count of records in a table
tbl_count = DBI::dbGetQuery(db_con_local,
                            "select count(*) from survey_type_lut")
DBI::dbDisconnect(db_con_local)

## End(Not run)

arestrom/repg documentation built on May 9, 2019, 2:25 p.m.