LoginSignup
8
14

More than 5 years have passed since last update.

0からさくらVPSでPostgresSQLサーバを構築

Last updated at Posted at 2018-02-15

目標

さくらVPSでCentOS7の環境でPostgresSQLをインストールし外部接続できるようにする

※PostgresSQLは好きなバージョンを入れる。バージョンにあわせてパスの数字を変えること。

以下のサイトでPostgresSQLのバージョンのURLをコピーしてコマンドに入力する
https://yum.postgresql.org/repopackages.php

Postgres.png

PostgresSQLダウンロード(URLは上記のサイトから取得)
sudo yum -y install https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7-x86_64/pgdg-centos94-9.4-3.noarch.rpm
PostgresSQL周りのツールをインストール
sudo yum -y groupinstall "PostgreSQL Database Server 9.4 PGDG"
PostgresSQLの初期化
/usr/pgsql-9.4/bin/postgresql94-setup initdb

Initializing database ... OK
PostgresSQLの起動
sudo systemctl start postgresql-9.4.service
PostgresSQLの起動(システム 起動時)
sudo systemctl start postgresql-9.4.service
PostgresSQLのユーザパスワード設定
sudo passwd postgres

Changing password for user postgres.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
実行ユーザをpostgresに変える
su - postgres
PostgresSQLの実行画面に移動
bash-4.2$ psql
could not change directory to "/root": Permission denied
psql (9.4.16)
Type "help" for help
PostgresSQLのユーザのパスワード変更とバージョン確認
postgres=# \password
Enter new password:
Enter it again:

postgres=# SELECT version();
                                                    version
--------------------------------------------------------------------------------
--------------------------------
 PostgreSQL 9.4.16 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.8.5 2015
0623 (Red Hat 4.8.5-16), 64-bit
(1 row)

#PostgresSQL終了
postgres=# \q
PostgresSQLの外部接続のため設定変更
vi /var/lib/pgsql/9.4/data/postgresql.conf

#追加箇所
listen_addresses = '*'
#listen_addresses = 'localhost'         # what IP address(es) to listen on;
PostgresSQLの外部接続のため設定変更(解放)
vi /var/lib/pgsql/9.4/data/postgresql.conf

# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
#追加箇所
host all all 0.0.0.0/0 md5
PostgresSQLを再機動:読み込み直し
sudo systemctl restart postgresql-9.4.service
ポート番号を開ける&ファイアーウォール再機動
sudo firewall-cmd  --permanent --add-port=5432/tcp
sudo firewall-cmd --reload
開いてるポート確認
firewall-cmd --list-all

外部との接続確認:A5M2というツールを使用しゲストPCからの外部接続に問題ないこと確認

Postgres2.png

8
14
1

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
8
14