LoginSignup
0
0

More than 3 years have passed since last update.

PostgreSQL 基礎~インストール

Last updated at Posted at 2021-03-31

まず始めに

 今回はPostgreSQLの基礎~インストールについてまとめてみました。

 

PostgreSQLとは?

 近年シェアが伸びてきている人気の無償DBMSで、 商用利用の場合も無料で利用できます。
 LinuxなどのUNIX互換OSはもちろんのこと、Windowsにも対応しています。
 postgresプロジェクトから始まって30年以上経ち、世界中の開発者により改良が続けられ、
 現在では有償RDBMSと比べても遜色ない性能、機能、信頼性を持ち合わせるRDBMSとなっています。

クライアントツール

 代表的なクライアントツールは2種類

paql

コマンドシェル。postgresqlインストール時に同時にインストールされる。

psql -U ユーザ名 -h ホスト名 -l
    データベース一覧の表示

psql -U ユーザ名 -h ホスト名 -d DB名 
    データベースに接続

\h 
    SQLコマンドの文法ヘルプ * で全コマンドを表示
\x 
    拡張表示ON/OFF。クエリー結果のカラムが縦に表示されて見やすくなる。
\i [ファイルパス]
    ファイルからコマンドを読み込んで実行
\dt
    テーブル一覧を表示
\du
    ユーザー一覧を表示
\c [データベース名]
    別のデータベースに接続する

pgAdmin

WindowsなどからGUIでPostgreSQLを操作することができます。
 オブジェクトがツリー表示され、右クリックからオブジェクトを操作できます。
 基本構造を把握できるため、CLI派も最初は使っておくとよいかもしれません。

PostgreSQLのインストール

 今回はCentOS7上にPostgreSQLをインストールする想定です。
 まずは公式サイトでバージョンを選択(今回は9.5)。
 「9.5」と書かれたテキストリンクをクリックし、「CentOS 7 – x86_64」と書かれたテキストリンクからURLをコピーなどでメモ帳などに控えます。(ここでダウンロードはしない)

$wget https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-2.noarch.rpm
download.postgresql.org (download.postgresql.org) をDNSに問いあわせています... 213.189.17.228, 217.196.149.55, 87.238.57.227, ...
download.postgresql.org (download.postgresql.org)|213.189.17.228|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 5460 (5.3K) [application/x-redhat-package-manager]
`pgdg-centos95-9.5-2.noarch.rpm' に保存中

100%[======================================>] 5,460       --.-K/s 時間 0s

2016-04-30 11:43:42 (114 MB/s) - `pgdg-centos95-9.5-2.noarch.rpm' へ保存完了 [5460/5460]

※Wgetコマンドがない場合は下記でインストール。
$ yum -y install wget

$ rpm -ivh pgdg-centos95-9.5-2.noarch.rpm
警告: pgdg-centos95-9.5-2.noarch.rpm: ヘッダー V4 DSA/SHA1 Signature、鍵 ID XXXXXXXX: NOKEY
準備しています...              ################################# [100%]
更新中 / インストール中...
   1:pgdg-centos95-9.5-2              ################################# [100%]
~

$ ls -l /etc/yum.repos.d/
 -rw-r--r--. 1 root root  482 10月 21  2015 pgdg-95-centos.repo

$ yum -y install postgresql95-server postgresql95-devel postgresql95-contrib

~
インストール:
  postgresql95-contrib.x86_64 0:9.5.2-1PGDG.rhel7
  postgresql95-devel.x86_64 0:9.5.2-1PGDG.rhel7
  postgresql95-server.x86_64 0:9.5.2-1PGDG.rhel7

依存性関連をインストールしました:
  libxslt.x86_64 0:1.1.28-5.el7
  postgresql95.x86_64 0:9.5.2-1PGDG.rhel7
  postgresql95-libs.x86_64 0:9.5.2-1PGDG.rhel7

完了しました!

$ /usr/pgsql-95/bin/postgres --version
postgres (PostgreSQL) 9.5

 以上で、PostgreSQLのインストールは完了です。

 インストール後は下記コマンドで初期化し、自動起動の設定を行います。

$ /usr/pgsql-10/bin/postgresql-10-setup initdb
Initializing database ... OK

$ systemctl enable postgresql-9.5
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-9.5.service to /usr/lib/systemd/system/postgresql-9.5.service.

起動と停止

 起動・停止はsystemctlで行います。

#起動 
$ systemctl start postgresql-9.5

#停止
$ systemctl stop postgresql-9.5

#再起動
$ systemctl restart postgresql-9.5

デフォルト設定

 設定ファイル:install_dir/data/postgresql.conf
 接続制限 :install_dir/data/pg_hba.conf
 ログファイル:install_dir/data/pg_log

 ユーザ:postgres()
 ポート:5432

$ sudo -u postgres psql -U postgres
could not change directory to "/root"
psql (10.1)
Type "help" for help.

postgres=# 

#ログアウト
postgres=# \q

 以上でPostgreSOLに接続できることが確認できました。

最後に

 今回はPostgreSQLのインストールまでをまとめてみました。
 次回は設定や操作についてまとめようと思います。
 
 最後まで読んでいただきありがとうございました。

0
0
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
0