LoginSignup
12
13

More than 5 years have passed since last update.

PostgreSQLのテーブルをRのデータフレームに一瞬で流しこむ方法

Posted at

はじめに

PostgreSQLのデータをRで扱いたいことがあったので備忘録として置いておきます。

作業環境

Mac OSX Yosemite Version 10.10.5
Rstudio Version 0.99.491
R Version 3.2.3
PostgreSQL Version 9.5.0
RPostgreSQL Version 0.4

方法

Rstudioから、RPostgreSQLパッケージを使用して行います。
PostgreSQLはclient versionを使用しており、両方localhostにある状況です。

コード

readPSQL
# RPostgreSQLインストール
install.packages("RPostgreSQL")
# RPostgreSQL読み込み
require(RPostgreSQL)
# RPostgreSQLを使って、PoststgreSQLに接続
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv,dbname="dbname",host="localhost",port=5432,user="username",password="password")
# 目的のテーブルがあるか確かめる
dbExistsTable(con, "tablename")
# "TRUE"
# myTableというdataframeにSQLのテーブルを流しこむ
myTable <- dbReadTable(con,"tablename")
12
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13