PostgresSQLのインストールの紹介です。PostgresSQLはソースからインストールします。
PostgresSQLのダウンロードはこちら
ユーザーとグループの作成
PostgreSQLはrootで起動できないので、ユーザとグループの作成をします。インストール先のフォルダーも作成します。
[root@localhost ~]# groupadd postgres
[root@localhost ~]# useradd -g postgres postgres
[root@localhost ~]# mkdir /usr/local/pgsql
[root@localhost ~]# chown -R postgres:postgres /usr/local/pgsql
インストール
PostgresSQLを上記のサイトよりダウンロードし、適当な場所で解凍します。
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# tar zxvf postgresql-9.2.3.tar.gz
先ほど、作ったユーザに切り替えてコンパイルします。
[root@localhost src]# su – postgres
[postgres@localhost postgresql-9.2.3]$ cd /usr/local/src/postgresql-9.2.3
[postgres@localhost postgresql-9.2.3]$ ./configure –enable-nls –prefix=/usr/local/pgsql
[postgres@localhost postgresql-9.2.3]$ gmake
[postgres@localhost postgresql-9.2.3]$ gmake check
[postgres@localhost postgresql-9.2.3]$ gmake install
「configure」時に下記のエラーが出ました。同じエラーが出た場合は参考にしてください。
環境変数の設定
「postgres」ユーザーで作業を行います。.bashrcファイルに下記の内容を追記します。
[postgres@web postgresql-9.2.3]$ vi /home/postgres/.bashrc
DBの初期化
データベースの初期作業です。「–encoding」は、お使いの文字コードを指定してください。「–no-locale」は、ロケールを使用しないことを設定するオプションです。ロケールとは、言語や文化に応じた処理をするOSの機構です。ここでは使用しない設定にしました。「-D」はデータの保管場所です。
[postgres@localhost postgresql-9.2.3]$ /usr/local/pgsql/bin/initdb –encoding=UTF8 –no-locale -D /usr/local/pgsql/data
起動の確認と初期設定
PostgreSQLの起動
[postgres@localhost ~]$ /usr/local/pgsql/bin/pg_ctl start
ログインとパスワードの変更
[postgres@localhost ~]$ psql
postgres=# SELECT * FROM pg_user;
postgres=# ALTER USER postgres with unencrypted password ‘newpassword’;
文字コードの確認
postgres=# select datname, pg_encoding_to_char(encoding) from pg_database;
datname | pg_encoding_to_char
———–+———————
template1 | UTF8
template0 | UTF8
postgres | UTF8
webshop | UTF8
(4 行)
自動起動
OSの起動時にPostgreSQLが起動するように設定をします。「root」ユーザーで設定します。
[root@localhost ~]# cp /usr/local/src/postgresql-9.2.3/contrib/start-scripts/linux /etc/rc.d/init.d/postgresql
[root@localhost ~]# chmod 755 /etc/rc.d/init.d/postgresql
[root@localhost ~]# chkconfig –add postgresql
[root@localhost ~]# chkconfig –list postgresql
postgresql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
(注意)
お使いのOSによっては、「chkconfig」ではなく、「systemctl」で設定する場合があるので注意してください。
(参考までに )
WEBサーバーを構築するに当たって、Apache、PHPが必要になる場合は、下記のURLを参考にしてください。
phpのインストールと設定
apacheのインストールと設定