0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python3 - Redshiftへの接続 - Mac

Last updated at Posted at 2018-08-08

#PostgrelSQLをmacに入れる

brew install postgresql

#psycopg2(postgersqlに接続するためのライブラリ)の使用

pip install psycopg2

#sshポートにstepで接続
#Redshiftからデータを取得
#使い方

#psycopg2をインポート
import psycopg2 as psy2
# コネクション作成
conn = psy2.connect(dbname="", user="", password="", host="", port="")
# カーソル作成
cur = conn.cursor()
# テスト
test1 = "select * from logs;"
# SQL結果を受け取る
cur.execute(test1)
#結果の最初の行だけ取ってくる
result1 = cur.fetchone()
# 結果の表示
result1
#全ての結果を取得する
result2 = cur.fetchall()
# 結果の表示
result2
# クローズ
cur.close()
conn.close()
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?