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 に Posgtgres13 をインストールしてみる

Last updated at Posted at 2020-12-15

目的

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

ファイルのダウンロード

PostgreSQL Database Downloadから
13,1 Windows x86-64 をDLする
インストールを始める前にロケール(国際化と地域化)をまず読んで--locale=Cでいいのかな?と事前に確認したほうが良いのかな?と
※ただ10年以上前の記事であること、Ubuntuのデフォで --locale=ja_JP.UTF-8 でインストールされるんだよなぁ・・
OSは異なるが、以下も参考にする。PostgreSQLにおけるロケールの影響調査
password_encryption = scram-sha-256 とデフォルト値が変更になったようだ
※A5M2は対応済 > PostgreSQL scram-sha-256 の対応
※phpでError:SQLSTATE[08006] [7] SCRAM authentication requires libpq version 10 or above が発生した場合 php8\libpq.dll を c:\Windows 以下にコピーしているかどうかを確認すること
PHPのWindows環境でphp-pgsql.dllが認識されない件について

インストール

※基本的なインストールの手順はこちらPostgreSQLのダウンロード及びインストール
※キャプチャ付きは他にも色々見つかるはず
※インストール先は、好みで
postgresql-13.1-1-windows-x64.exe をダブルクリック
Installation Directory : C:\Program Files\PostgreSQL\13
Select Components : そのまま
Data Directory : C:\Program Files\PostgreSQL\13\data
Passwd : 適当に
Port : 5432
Locale : C
Pre Installation Summary を確認
インストール開始
インストール終了後 C:\Program Files\PostgreSQL\13\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
で作成した状態かなと

設定ファイルの修正

PostgreSQL にリモートホストから安全に接続するにはを参考に


C:\Program Files\PostgreSQL\13\data\postgresql.conf はデフォルトのまま
listen_addresses = '*'
#authentication_timeout = 1min		# 1s-600s
password_encryption = scram-sha-256		# md5 or scram-sha-256

C:\Program Files\PostgreSQL\13\data\pg_hba.conf を以下に修正
# "local" is for Unix domain socket connections only
local   all             all                                     scram-sha-256
# IPv4 local connections:
host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             192.168.5.0/24          scram-sha-256
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     scram-sha-256
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

修正後再起動する

サンプル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
パスワード:

米 psql (14.1) で以下の様になるが、データは作成されている(ようだ)
 13以下で試した時は出ていなかった気が・・・
PS C:> pg_restore -U postgres -d dvdrental dvdrental.tar
パスワード:
pg_restore: TOC処理中:
pg_restore: TOCエントリ6; 2615 2200 SCHEMA public postgres から
pg_restore: エラー: could not execute query: ERROR:  schema "public" already exists
コマンド: CREATE SCHEMA public;
米

PS > psql -U postgres
ユーザ postgres のパスワード:
psql (13.1)
"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 (13.1)
"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 > python -V
Python 3.9.0

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のダウンロード及びインストール
PostgreSQL Sample Database
Ubuntu 20.04 に Posgtgres をインストール後 C# + Npgsql でアクセスしてみる
Win10 + VsCode + Python3 + psycopg2 から Ubuntu 20.04 + Posgtgres12 にアクセスしてみる
PostgreSQL にリモートホストから安全に接続するには
Windows 10 に 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?