0
1

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 1 year has passed since last update.

Windows 10 に Posgtgres12 をインストールしてみる

Last updated at Posted at 2020-06-25

目的

Windows 10 に Posgtgres12 をインストールしてみる

ファイルのダウンロード

PostgreSQL Database Downloadから
12.3xWindows x86-64 をDLする
インストールを始める前にロケール(国際化と地域化)をまず読んで--locale=Cでいいのかな?と事前に確認したほうが良いのかな?と
※ただ10年以上前の記事であること、Ubuntuのデフォで --locale=ja_JP.UTF-8 でインストールされるんだよなぁ・・
※TODO 最近はどうなんだろう??

インストール

※基本的なインストールの手順はこちらPostgreSQLのダウンロード及びインストール
※キャプチャ付きは他にも色々見つかるはず
※インストール先は、好みで
postgresql-12.3-1-windows-x64.exe をダブルクリック
Installation Directory : D:\DB\PostgreSQL\12
Select Components : そのまま
Data Directory : D:\DB\PostgreSQL\12\data
Passwd : 適当に
Port : 5432
Locale : C
Pre Installation Summary を確認
インストール開始
インストール終了後 D:\DB\PostgreSQL\12\bin をPATHに追加する


PS > psql -lU postgres
ユーザ postgres のパスワード:
                                        データベース一覧
   名前    |  所有者  | エンコーディング | 照合順序 | Ctype(変換演算子) |     アクセス権限
-----------+----------+------------------+----------+-------------------+-----------------------
 postgres  | postgres | UTF8             | C        | C                 |
 template0 | postgres | UTF8             | C        | C                 | =c/postgres          +
           |          |                  |          |                   | postgres=CTc/postgres
 template1 | postgres | UTF8             | C        | C                 | =c/postgres          +
           |          |                  |          |                   | postgres=CTc/postgres
(3 行)

psql -l の結果を見ると
initdb --locale=C --encoding=UTF-8
で作成した状態かなと

設定ファイルの修正


D:\DB\PostgreSQL\12\data\postgresql.conf はデフォルトのまま
listen_addresses = '*'

D:\DB\PostgreSQL\12\data\pg_hba.conf を以下に修正
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             192.168.5.0/24          md5
# IPv6 local connections:
host    all             all             ::1/128                md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
host    replication     all             127.0.0.1/32           md5
host    replication     all             ::1/128                md5

修正後再起動する

サンプルDBの作成

PostgreSQL Sample Databaseより
PostgreSQL DVD Rental sample databaseをDL後 dvdrental.zip を解凍する


PS > createdb --locale=C --encoding=UTF-8 --template=template0 -U postgres dvdrental
パスワード:
PS > psql -lU postgres
ユーザ postgres のパスワード:
                                        データベース一覧
   名前    |  所有者  | エンコーディング | 照合順序 | Ctype(変換演算子) |     アクセス権限
-----------+----------+------------------+----------+-------------------+-----------------------
 dvdrental | postgres | UTF8             | C        | C                 |
 postgres  | postgres | UTF8             | C        | C                 |
 template0 | postgres | UTF8             | C        | C                 | =c/postgres          +
           |          |                  |          |                   | postgres=CTc/postgres
 template1 | postgres | UTF8             | C        | C                 | =c/postgres          +
           |          |                  |          |                   | postgres=CTc/postgres
(4 行)

PS > pg_restore -U postgres -d dvdrental dvdrental.tar
パスワード:

PS > psql -U postgres
ユーザ postgres のパスワード:
psql (12.3)
"help"でヘルプを表示します。

postgres=# \c dvdrental
データベース"dvdrental"にユーザ"postgres"として接続しました。
dvdrental=# \d
                       リレーション一覧
 スキーマ |            名前            |     型     |  所有者
----------+----------------------------+------------+----------
 public   | actor                      | テーブル   | postgres
 public   | actor_actor_id_seq         | シーケンス | postgres
 public   | actor_info                 | ビュー     | postgres
 public   | address                    | テーブル   | postgres
 public   | address_address_id_seq     | シーケンス | postgres
 public   | category                   | テーブル   | postgres
~
dvdrental=# \q
PS >

アクセス環境の設定


PS > psql -U postgres
ユーザ postgres のパスワード:
psql (12.3)
"help"でヘルプを表示します。

postgres=# create role dvdrental with login password 'passwd';
CREATE ROLE
postgres=# \c dvdrental
データベース"dvdrental"にユーザ"postgres"として接続しました。

postgres=# grant all on all tables in schema public to dvdrental;
GRANT
postgres=# grant all on all sequences in schema public to dvdrental;
GRANT
postgres=# grant all on all functions in schema public to dvdrental;
GRANT

psycopg2のインストール


PS > pip3 install psycopg2

python3 からアクセスしてみる

# Windows Add env PYTHONIOENCODING = UTF-8 & restart vscode
# coding:utf-8

import psycopg2

server   = '192.168.5.xx' 
port= '5432'
database = 'dvdrental' 
username = 'dvdrental' 
password = 'demo'

# 接続文字列 - 空白文字がセパレータなのか??
constr = 'host=' + server + ' port=' + port + ' dbname=' + database + ' user=' + username + ' password=' + password
conn = psycopg2.connect(constr)

cur = conn.cursor()

# 件数を確認する
cur.execute("SELECT COUNT(*) FROM actor")
row = cur.fetchone()

if row:
    print(row)

cur.close()
conn.close()

参考にしたサイトはこちら

PostgreSQL Database Download
ロケール(国際化と地域化)
PostgreSQLのダウンロード及びインストール
PostgreSQL Sample Database
Ubuntu 20.04 に Posgtgres をインストール後 C# + Npgsql でアクセスしてみる
Win10 + VsCode + Python3 + psycopg2 から Ubuntu 20.04 + Posgtgres12 にアクセスしてみる

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?