LoginSignup
3

More than 5 years have passed since last update.

PostgreSQL 9.4.4をソースコードからインストールする

Posted at

前回に引き続き、CentOS 7での環境構築です。
今回は、PostgreSQLをソースコードからインストールします。
Hadoopのインストールは若干気合がいるので後回し。。。

準備

makegccなどがつかえるようにしておきます。
入っていなければyum installで入れます。

$ make --version
GNU Make 3.82
Built for x86_64-redhat-linux-gnu
Copyright (C) 2010  Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ gcc -v
組み込み spec を使用しています。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.3/lto-wrapper
ターゲット: x86_64-redhat-linux
configure 設定: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.3-20140911/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.3-20140911/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
スレッドモデル: posix
gcc バージョン 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC)

また、PostgreSQLのコンパイルに必要なライブラリを入れておきます。

  • zlib-devel
  • readline-devel

ソースコードの入手・展開

http://www.postgresql.org/download/ からPostgreSQLのソースコードをダウンロードして適当な場所に配置します。今回は9.4系の最新版9.4.4を使います。

$ tar zxf postgresql-9.4.4.tar.gz
$ cd postgresql-9.4.4/

コンパイル

configureスクリプトを使って、コンパイル構成を定義します。

$ ./configure --prefix=/usr/local/pgsql/9.4.4 --enable-debug
  • --prefixオプションはインストール先ディレクトリを指定します
  • --enable-debugオプションはデバッグ用情報をつけてコンパイルするためにつけます

また、GDBでデバッグする際に混乱を招くので、コンパイラの最適化オプションをなくしておきます。
それにはsrc/Makefile.globalファイルの以下の場所を編集します。

src/Makefile.global
CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement     -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -    fwrapv -fexcess-precision=standard -g -O2

末尾のO2オプションを削除します。
configure時の--enable-debug指定により、-gオプションが付与されていますが、これを-g3オプションに変更しておきます。これにより、GDBでデバッグする際にマクロが実行できるようになります。PostgreSQLはマクロが要所要所で使われているので、これをしておくとかなり便利です。

-g3オプションについては、以下のリンク先を参考にしました。
http://www.clear-code.com/blog/2013/5/8.html
https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html

結局、変更後は以下のようになります。

src/Makefile.global
CFLAGS = -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement     -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -    fwrapv -fexcess-precision=standard -g3

コンパイル

makeします。-jオプションで並列度を指定しておきます。

$ make -j 2

インストール

$ su
# make install

環境変数設定

PostgreSQLを起動するユーザの環境変数を設定します。

.bashrc
export PGVERSION=9.4.4
export PGDATA=/home/postgres/pg_data/$PGVERSION
export POSTGRES_HOME=/usr/local/pgsql/$PGVERSION

export PGLIB=$POSTGRES_HOME/lib
export MANPATH="$MANPATH":$POSTGRES_HOME/share/man
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB":/usr/local/lib

export PATH=$POSTGRES_HOME/bin:$HOME/bin:$PATH

PGVERSIONは異なるバージョンのPostgreSQLをインストールした際に切り替えるための便宜用です。
PGDATAinitdbpg_ctlコマンド時にいちいち指定しないためのもの。
その他、共有ライブラリにインストールしたPostgreSQLを追加するためにLD_LIBRARY_PATHを設定し、manコマンド向けにMANPATHにも追加します。

最後に、PATHにインストールしたPostgreSQLの各種コマンドを追加します。

データベースクラスタ作成

$ initdb --no-locale
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default database encoding has accordingly been set to "SQL_ASCII".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /home/postgres/pg_data/9.4.4 ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /home/postgres/pg_data/9.4.4/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    postgres -D /home/postgres/pg_data/9.4.4
or
    pg_ctl -D /home/postgres/pg_data/9.4.4 -l logfile start

設定

とりあえず、PostgreSQLサーバログをファイルに出すようにしておきます。

postgresql.conf
logging_collector = on

PostgreSQLサーバ起動

$ pg_ctl start
server starting
LOG:  redirecting log output to logging collector process
HINT:  Future log output will appear in directory "pg_log".
$ pgrep -a postgres
5457 /usr/local/pgsql/9.4.4/bin/postgres
5458 postgres: logger process
5460 postgres: checkpointer process
5461 postgres: writer process
5462 postgres: wal writer process
5463 postgres: autovacuum launcher process
5464 postgres: stats collector process

psqlで接続してみる

$ psql -Upostgres -d postgres
psql (9.4.4)
Type "help" for help.

postgres=#

とりあえずこれで使えるようになりました。
Hadoopと違ってソースコードからビルドしても数分で終わるので、気軽です。

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
3