14
15

More than 5 years have passed since last update.

railsからredshiftに接続する

Posted at

postgresqlをインストール

sudo yum install postgresql postgresql-devel

activerecord-redshift-adapterをインストール

Gemfile
gem "activerecord-redshift-adapter"

インストールする

bundle install

database.ymlに接続情報を記載

database.yml
redshift:
  adapter: redshift
  database: applog
  host: *****.**********.ap-northeast-1.redshift.amazonaws.com
  port: 5439
  username: app
  password: 
  encoding: utf8
  pool: 5
  timeout: 30000 # タイムアウトは長めに

SQL実行

ActiveRecord::Base.establish_connection(:redshift)
result = ActiveRecord::Base.connection.execute "select * from table_name where log_date='#{d.strftime("%Y-%m-%d")}'"
result.each{|record|
    p record # hash
}

その他

  • ActiveRecordでも使えるようですが試してません
  • 基本的にはrakeやrails runnerで使うものかと思います。
  • rails4で動作しました。
14
15
5

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
14
15