A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/poissonconsulting/psql below:

poissonconsulting/psql: An R package for interacting with PostgreSQL

psql is a wrapper on DBI and PostgreSQL commands thus eliminating the need for users to have to learn PostgreSQL and can just use R.

psql is an R package that

# install.packages("devtools")
devtools::install_github("poissonconsulting/psql")
Install postgres locally by running the following

Your database connection details should be stored in a config.yml file. Set the psql.config_path option as the file path to the config file. Set the psql.config_value option as the value in the config file.

Example of a config.yml file:

default:
  user: "postgres"
  host: 127.0.0.1
  dbname: "postgres"
  port: 5432
  password: !expr Sys.getenv("DM_PASSWORD")
connecting to multiple databases

You can use a single config file to connect to multiple databases. You must have a default connection as shown in both examples.

default:
  user: "postgres"
  host: 127.0.0.1
  dbname: "postgres"
  port: 5432
  password: !expr Sys.getenv("DM_PASSWORD")

data_user:
  user: "data_analyst"
  host: 127.0.0.1
  dbname: "postgres"
  port: 5432
  password: !expr Sys.getenv("DATA_ANALYST_PASSWORD")

To access the database with the data analyst credentials set the psql.config_value to "data_user" from the default option "default".

A .pgpass file is required (if your database has a password) when using the psql_backup() function. The postgresql website provides details on how to set it up.

Currently add the following file named .pgpass to your home directory (~)

and then in the terminal run chmod 0600 ~/.pgpass

Start by setting the file path for your config file in options so you do not have to pass the config file each time.

library(psql)
options(psql.config_path = "config.yml")

Lets create a schema and table with psql_execute_db()

psql_execute_db("CREATE SCHEMA truck")
## [1] 0
psql_execute_db(
  "CREATE TABLE truck.model (
     name TEXT NOT NULL,
     code INTEGER)"
)
## [1] 0

When you need to add data to a table use psql_add_data(), and the number of rows added will be output.

model <- data.frame(
  name = c("Ranger", "F-150", "F-250"),
  code = c(2457, 1475, 1247)
)

psql_add_data(model, schema = "truck")
## [1] 3

To list all the tables in the schema use psql_list_tables().

psql_list_tables(schema = "truck")
## [1] "model"

To read a table in from your database use psql_read_table()

truck_models <- psql_read_table(tbl_name = "model", schema = "truck")
truck_models
##     name code
## 1 Ranger 2457
## 2  F-150 1475
## 3  F-250 1247

To copy and save your database use psql_backup()

psql_backup("~/Database_backups/db_trucks_2020-07-19.sql")

Please report any issues.

Pull requests are always welcome.

Please note that the psql project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

In your local server you need to have a database called mydb for the tests to work.

You also need the hosted test database credentials in your .Renviron file.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4