LoginSignup
0
1

More than 5 years have passed since last update.

Python3からPostgreDBに接続する方法

Last updated at Posted at 2018-10-27

個人作業用にメモしてます。

Python3からPostgresqlを実行する方法

#接続用ライブラリ
import psycopg2

#PostgreDBに接続
conn = psycopg2.connect("host=[ホスト名] port=[ポート番号] dbname=【データベース名】 user=【ユーザー名】 password=【パスワード】")

#SQLを実行
cur = conn.curcur
cur.execute("【SQL文】")

DB接続設定を発行する方法を改良

##############################
#    Connection Settings     #
##############################
host     = "【ホスト名】"
port     = "【ポート番号】"
dbname   = "【データベース名】"
user     = "【ユーザー名】"
password = "【パスワード】"

##############################
#    Connection Exe          #
##############################
conn = psycopg2.connect("host={h} port={p} dbname={db} user={u} password={pa}"\
   .format(h=host,p=port,db=dbname,u=user,pa=password))
0
1
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
1