PythonからPostgreSQLを操作
■Postgreをwindows 10にinstall
$ sudo apt update
$ sudo apt install postgresql
再起動
$ sudo /etc/init.d/postgresql restart
ユーザpostgresに切り替え
$ sudo -u postgres -i
ユーザuserを作成, パスワードを聞かれるのでパスワードを設定する
$ createuser -d -U postgres -P user
user用データベースを作成
$ createdb userdb –encoding=UTF-8 –owner=user
■psycopg2をインストール
pythonからPostgreSQLを操作するためpsycopg2をインストール
$pip install psycopg2
エラーが出力される場合は以下コマンドを実行
$ pip install psycopg2-binary
$ sudo apt install libpq-dev
$ sudo apt install postgresql-common
コマンドを実行したら再度インストール
$ pip install psycopg2
■DataBaseに接続
参考
Windows10のWSL(Ubuntu)にPostgreSQLをインストールしたときのメモ
psycopg2 インストールエラー
初心者がPythonを使ってPostgreSQL に接続する
psycopg2 でよくやる操作まとめ