LoginSignup
0
0

More than 1 year has passed since last update.

PythonでAzure Database for PostgreSQLに接続する

Posted at

PythonでAzure Database for PostgreSQLに接続する備忘録。

準備1(接続情報を取得)

  1. Azure portal にアクセスする
  2. サーバーの [概要] ページで、[サーバー名]と[管理者ユーザー名]を確認する
    image.png

準備2(Python:psycopg2のインストール)

  1. pipでpsycopg2をインストールする

サンプル

# -*- coding: utf-8 -*-
import psycopg2

HOST = "<server-name>"      # [サーバー名]
USER = "<admin-username>"   # [管理者ユーザー名]
PASSWORD = "<admin-password>"   # パスワード

PORT = "5432"               # ポート
DB_NAME = "<database-name>" # DB名

if __name__ == '__main__':
    detabase_url = "host={} port={} dbname={} user={} password={}".format(HOST, PORT, DB_NAME, USER, PASSWORD)
    with psycopg2.connect(detabase_url) as connection:
        with connection.cursor() as cursor:
            sql = 'SELECT * XXX' # 実行したいSQL
            cursor.execute(sql)
            rows  = cursor.fetchall()
            print (rows)

参考

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