2018年5月15日火曜日

Ubuntu18.04(WSL)へのPostgreSQL9.6インストール

1. パッケージのダウンロード一覧にPostgresを追記
$ sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' > /etc/apt/sources.list.d/pgdg.list"

2. 公開鍵を信頼キーリストに追加
$ wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
$ apt-key list

3. パッケージを更新
$ sudo apt -y update

4. インストール実行
$ sudo apt install -y postgresql-9.6

5.設定ファイルの変更
$ sudo vim /etc/postgresql/9.6/main/postgresql.conf
#listen_addresses = 'localhost'
↓
listen_addresses = '*'

$ sudo vim /etc/postgresql/9.6/main/pg_hba.conf
host    all             all             127.0.0.1/32              trust
host    all             all             localhost                 trust

6.「postgres」ユーザーへのパスワード設定
$ sudo passwd postgres

7.サービスコマンド
$ sudo service postgresql start
$ sudo service postgresql stop
$ sudo service postgresql restart

WSL(Windows Subsystem for Linux)Ubuntu18.04 初期設定

1.インストール後の初回アップデート
$ sudo apt -y update
$ sudo apt -y upgrade
$ sudo apt -y autoremove
$ exit

2.日本語化
$ sudo apt install -y language-pack-ja
$ sudo update-locale LANG=ja_JP.UTF-8
$ exit

3.その他
$ sudo su -

2018年5月14日月曜日

PostgreSQL9.6.9 ソースからのインストール 3/3

$ su -

ファイヤーウォールの設定
PostgreSQLへのアクセスを有効にします。
# firewall-cmd --add-service=postgresql --zone=public --permanent
# systemctl restart firewalld

SELinuxの設定
PostgreSQLへのアクセスを有効にします。
# setsebool -P postgresql_can_rsync on
# setsebool -P postgresql_selinux_transmit_client_label on
# setsebool -P selinuxuser_postgresql_connect_enabled on

確認
# getsebool -a | grep postgresql

自動起動設定
サービス定義ファイルを作成します。
#vi /usr/lib/systemd/system/postgresql.service
# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades.  If you want to customize, the
# best way is to create a file "/etc/systemd/system/postgresql.service",
# containing
#   .include /lib/systemd/system/postgresql.service
#   ...make your changes here...
# For more info about custom unit files, see
# http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F

# For example, if you want to change the server's port number to 5433,
# create a file named "/etc/systemd/system/postgresql.service" containing:
#   .include /lib/systemd/system/postgresql.service
#   [Service]
#   Environment=PGPORT=5433
# This will override the setting appearing below.

# Note: changing PGPORT or PGDATA will typically require adjusting SELinux
# configuration as well; see /usr/share/doc/postgresql-*/README.rpm-dist.

# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-setup.

# Note: in F-17 and beyond, /usr/lib/... is recommended in the .include line
# though /lib/... will still work.

[Unit]
Description=PostgreSQL database server
After=network.target

[Service]
Type=forking

User=postgres
Group=postgres

# Port number for server to listen on
Environment=PGPORT=5432

# Location of database directory
Environment=PGDATA=/usr/local/pgsql/data

# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog

# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000

#ExecStartPre=/usr/local/pgsql/bin/postgresql-check-db-dir ${PGDATA} <=このモジュールは存在しないのでコメント
ExecStart=/usr/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D ${PGDATA} -s

# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300

[Install]
WantedBy=multi-user.target

EOF

サービスでの起動と停止
# systemctl start postgresql
# systemctl stop postgresql
# systemctl enable postgresql

最後に、PGAdmin等から接続出来るか確認します。

2018年4月24日火曜日

PostgreSQL9.6.9 ソースからのインストール 2/3

設定ファイルの編集
postgresql.conf
$ vi /usr/local/pgsql/data/postgresql.conf

リモートアクセス可能にする。
#listen_addresses = 'localhost'

listen_addresses = '*'

明示的にポートを指定する。
#port = 5432

port = 5432

ログを有効にする。
#logging_collector = off

logging_collector = on

ログのファイル名を変更する。
#log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'

log_filename = 'postgresql-%d.log'

#log_line_prefix = ''

log_line_prefix = '%t [%p] '

pg_hba.conf
$ vi /usr/local/pgsql/data/pg_hba.conf
リモートアクセス可能にする。
host    all             all             127.0.0.1/32            trust

host    all             all             192.168.1.0/24            trust

設定の反映
$ pg_ctl restart -w

DBのユーザ作成
$ createuser -s -d -r -P pgsa

作成したユーザを確認する。
$ psql -U postgres
\du

DBの設定は以上です。次回はOS側の設定になります。

2018年4月23日月曜日

PostgreSQL9.6.9 ソースからのインストール 1/3

わざわざソースからインストールする必要性はないのですが、勉強がてらソースからインストールしました。

まずはPostgreSQLが導入されているか確認します。
# rpm -qa | grep postgres

もしPostgreSQLが存在していたら、次のコマンドで削除しておきます。
# sudo yum -y remove postgresql

コンパイルに必要なgcc、readline-devel、zlib-develをインストールします。
# yum -y install gcc
# yum -y install readline-devel zlib-devel

PostgreSQL のコンパイルや実行に使用するユーザ postgres を作成します。
# useradd postgres
# passwd postgres

ソースコードをダウンロードします。
# cd /usr/local/src/
# wget https://ftp.postgresql.org/pub/source/v9.6.9/postgresql-9.6.9.tar.gz

展開します。
# tar xzf postgresql-9.6.9.tar.gz

オーナーを変えるます。
# chown -R postgres /usr/local/src/postgresql-9.6.9

インストール先ディレクトリを作成し、オーナーを変えます。
# mkdir /usr/local/pgsql9.6.9
# chown postgres /usr/local/pgsql9.6.9

今後発生するVupによるパス差分をシンボリックリンクで吸収します。
# unlink /usr/local/pgsql
# ln -s /usr/local/pgsql9.6.9 /usr/local/pgsql

コンパイルの準備として、スイッチユーザーとディレクトリ移動します。
# su - postgres
$ cd /usr/local/src/postgresql-9.6.9

インストール先を指定して、Makefileを生成します。
$ ./configure --prefix=/usr/local/pgsql

コンパイル
$ make world

インストール
$ make install-world

worldを指定しますと、contribモジュールやドキュメントを含めてコンパイルします。

postgresユーザーの環境変数を設定します。
(postgresユーザー以外を使用する場合はそのユーザーも設定する。)
$ vi /home/postgres/.bash_profile
以下の内容を最下部に書いて保存します。
PGHOME=/usr/local/pgsql
export PATH=$PGHOME/bin:$PATH
export LD_LIBRARY_PATH=$PGHOME/LIB:$LD_LIBRARY_PATH
export MANPATH=$PGHOME/share/man:$MANPATH
export PGDATA=/usr/local/pgsql/data

環境変数を反映させます。
$ . ~/.bash_profile

ソースからのインストールは以上です。 DBの設定は次回に続きます。

2018年3月18日日曜日

CentOS 7.4 インストール

プロジェクトの最終的な公開先はAWSのAmazon Linuxになります。
なので、テスト環境はCentOSで構築します。
インストール方法はいろんなところで紹介されているので割愛します。

インストール後、下記コマンドで最新版にします。
# yum -y update

今後、各種ソフトをインストールしますので、wgetをインストールします。
# yum -y install wget


Linux初心者なので、取りあえず必要なコマンドをまとめます。
# shutdown -h now
# reboot

Linux + SpringMVC + PostgreSQL Webアプリプロジェクト

Linux + SpringMVC + PostgreSQL にてWebアプリを開発するプロジェクトに関わることになりました。
環境構築、開発時の備忘録を残していこう思っています。