0

【未経験者向け】PostgreSQL セットアップ(Windows向け)

More than 1 year has passed since last update.

PostgreSQL セットアップ

  • 知り合いの未経験者向けにSQLの学習が行える環境を作成する手順を提示する。

環境情報

  • Windows10(Client)

1.ダウンロード

  • PostgreSQLの公式ホームページからダウンロードページへリンクすることができる。

TopPage

01.JPG

DownloadPage

02.JPG

InstallerDownloadLink

03.JPG

インストーラのダウンロードページからダウンロードする。

※今回は、Version 9.6.8 / Windows 64bin向けを選択
download01.JPG

2.インストール

  • ダウンロードした「postgresql-9.6.8-2-windows-x64.exe」を起動して、ウィザードに従いインストールすればOK
  • 設定が必要なポイントは以下の5か所
    1. インストール場所(Installtion Directory)
      • PostgreSQLの本体が格納される。
    2. データファイル場所(Data Directory)
      • データベースの実体となるファイルが格納される。
    3. 初期ユーザ:postgres パスワード(Password/Retype Password)
      • 初期のSuperuserであるpostgresユーザのパスワード設定が必要。
    4. サービスポート(デフォルト:5432)
      • データベースのサービスポート
    5. ロケール設定:(デフォルト:ローカルロケール)
      • データベースの国情報設定(日本で使用する分には、Japanでよい)

3.PATHの設定

  • psqlやcreatedb などのコマンド利用のためにPATHを通しておく。

システム詳細設定から環境変数を追加

env2.JPG
addPath.JPG

4.確認

  • コマンドプロンプトから、以下のコマンドを実行し、バージョン確認できればOK
psql --version

version.JPG

おまけ:テストデータ投入

  • postgresql tutorial としてデータ「dvdrental」を想定したデータセットを投入してくれるsqlスクリプトが提供されている。

最初に、dvdrentalというデータベースを作成

createdb -U postgres dvdrental
  1. ダウンロードファイルを解凍する。
  2. 解凍するとdvdrental.tar があるのでこれを展開する。
  3. アーカイブを展開するとdatファイルとsqlスクリプトがあるため、これを実行する。
psql -U postgres -d dvdrental -f restore.sql

注意点

  1. 解凍直後のファイルは、権限変更が必要。   (dvdrentalフォルダ>セキュリティ>Everyoneにフルコン付与)
  2. restore.sqlは、datファイルパスが$$PATH$$となっているため、sqlファイルを直接修正し、フルパスアクセスできるようにする。

確認
データベース「dvdrental」にアクセスして、テーブル作成されていればOK

C:\>psql -U postgres dvdrental
ユーザ postgres のパスワード:
psql (9.6.8)
"help" でヘルプを表示します.

dvdrental=# \dt
               リレーションの一覧
 スキーマ |     名前      |    型    |  所有者
----------+---------------+----------+----------
 public   | actor         | テーブル | postgres
 public   | address       | テーブル | postgres
 public   | category      | テーブル | postgres
 public   | city          | テーブル | postgres
 public   | country       | テーブル | postgres
 public   | customer      | テーブル | postgres
 public   | film          | テーブル | postgres
 public   | film_actor    | テーブル | postgres
 public   | film_category | テーブル | postgres
 public   | inventory     | テーブル | postgres
 public   | language      | テーブル | postgres
 public   | payment       | テーブル | postgres
 public   | rental        | テーブル | postgres
 public   | staff         | テーブル | postgres
 public   | store         | テーブル | postgres
(15 行)


dvdrental=#
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away