トップページへ

PostgreSQLをLinuxでソースコードからビルドして利用する方法

小粋空間 » PostgreSQL » PostgreSQLをLinuxでソースコードからビルドして利用する方法

PostgreSQLをLinuxでソースコードからビルドして利用する方法を紹介します。

1.PostgreSQLソースのダウンロード

PostgreSQLのソースは、最新バージョンである下記のページにアクセスします(2017年8月現在)。

このページにある、"postgresql-9.6.3.tar.gz"をダウンロード。

2.ソースコードのビルド

ソースコードをビルドします(コマンドは赤色)。

# ./configure --without-readline
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
 :
config.status: linking src/backend/port/dynloader/linux.h to src/include/dynloader.h
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port
# make
make -C src all
make[1]: ディレクトリ `/home/y-araki/postgresql-9.6.3/src' に入ります
make -C common all
make[2]: ディレクトリ `/home/y-araki/postgresql-9.6.3/src/common' に入ります
make -C ../backend submake-errcodes
make[3]: ディレクトリ `/home/y-araki/postgresql-9.6.3/src/backend' に入ります
prereqdir=`cd 'utils/' >/dev/null && pwd` && \
          cd '../../src/include/utils/' && rm -f errcodes.h && \
          ln -s "$prereqdir/errcodes.h" .
make[3]: ディレクトリ `/home/y-araki/postgresql-9.6.3/src/backend' から出ます
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -O2 -DFRONTEND -I../../src/include -D_GNU_SOURCE  -DVAL_CONFIGURE="\"'--without-readline'\"" -DVAL_CC="\"gcc\"" -DVAL_CPPFLAGS="\"-DFRONTEND -D_GNU_SOURCE\"" -DVAL_CFLAGS="\"-Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -O2\"" -DVAL_CFLAGS_SL="\"-fpic\"" -DVAL_LDFLAGS="\"-L../../src/common -Wl,--as-needed -Wl,-rpath,'/usr/local/pgsql/lib',--enable-new-dtags\"" -DVAL_LDFLAGS_EX="\"\"" -DVAL_LDFLAGS_SL="\"\"" -DVAL_LIBS="\"-lpgcommon -lpgport -lz -lrt -lcrypt -ldl -lm  \""  -c -o config_info.o config_info.c
 :
make -C config all
make[1]: ディレクトリ `/home/y-araki/postgresql-9.6.3/config' に入ります
make[1]: `all' に対して行うべき事はありません.
make[1]: ディレクトリ `/home/y-araki/postgresql-9.6.3/config' から出ます
All of PostgreSQL successfully made. Ready to install.
# make install
make -C src install
make[1]: ディレクトリ `/home/y-araki/postgresql-9.6.3/src' に入ります
make -C common install
make[2]: ディレクトリ `/home/y-araki/postgresql-9.6.3/src/common' に入ります
make -C ../backend submake-errcodes
 :
make[1]: ディレクトリ `/home/y-araki/postgresql-9.6.3/src' から出ます
make -C config install
make[1]: ディレクトリ `/home/y-araki/postgresql-9.6.3/config' に入ります
/bin/mkdir -p '/usr/local/pgsql/lib/pgxs/config'
/usr/bin/install -c -m 755 ./install-sh '/usr/local/pgsql/lib/pgxs/config/install-sh'
/usr/bin/install -c -m 755 ./missing '/usr/local/pgsql/lib/pgxs/config/missing'
make[1]: ディレクトリ `/home/y-araki/postgresql-9.6.3/config' から出ます
PostgreSQL installation complete.

3.DBD::Pgインストール

Perlで利用したいため、DBD::Pgをインストールしました。

CPANからダウンロードしたDBD-Pg-3.6.2のアーカイブをLinux上で展開し、インストールします。

# cd DBD-Pg-3.6.2
# ls -l
合計 640
-rw-r--r--. 1 1000 1000  59734  5月 23 23:23 2017 Changes
drwxr-xr-x. 2 1000 1000   4096  5月 23 23:24 2017 LICENSES
-rw-r--r--. 1 1000 1000    747  4月 20 08:44 2017 MANIFEST
-rw-r--r--. 1 1000 1000    359  4月 20 08:44 2017 MANIFEST.SKIP
-rw-r--r--. 1 1000 1000   1675  5月 23 23:23 2017 META.yml
-rw-r--r--. 1 1000 1000  11177  5月 23 23:23 2017 Makefile.PL
-rw-r--r--. 1 1000 1000   8567  3月 29 07:03 2017 Pg.h
-rw-r--r--. 1 1000 1000 176872  5月 23 23:23 2017 Pg.pm
-rw-r--r--. 1 1000 1000  19749  4月  3 23:46 2017 Pg.xs
-rw-r--r--. 1 1000 1000  12217  5月 23 23:23 2017 README
-rw-r--r--. 1 1000 1000  30715  4月 26 11:19 2017 README.dev
-rw-r--r--. 1 1000 1000   2436  3月 29 06:10 2017 README.win32
-rw-r--r--. 1 1000 1000   3734  5月 23 23:24 2017 SIGNATURE
-rw-r--r--. 1 1000 1000   1887  3月 31 09:03 2017 TODO
-rw-r--r--. 1 1000 1000 162413  5月 23 23:22 2017 dbdimp.c
-rw-r--r--. 1 1000 1000  11296  4月  5 21:36 2017 dbdimp.h
-rw-r--r--. 1 1000 1000   2016  3月 29 06:10 2017 dbivport.h
drwxr-xr-x. 3 1000 1000   4096  5月 23 23:24 2017 lib
-rw-r--r--. 1 1000 1000  34222  4月 20 08:37 2017 quote.c
-rw-r--r--. 1 1000 1000   1501  3月 29 06:10 2017 quote.h
drwxr-xr-x. 3 1000 1000   4096  5月 23 23:24 2017 t
-rwxr-xr-x. 1 1000 1000   5419  4月  7 23:02 2017 testme.tmp.pl
-rw-r--r--. 1 1000 1000  42748  3月 29 07:03 2017 types.c
-rw-r--r--. 1 1000 1000   6198  3月 29 06:23 2017 types.h
-rw-r--r--. 1 1000 1000   2487  3月 29 06:10 2017 win32.mak
# perl Makefile.PL
Configuring DBD::Pg 3.6.2
PostgreSQL version: 90603 (default port: 5432)
POSTGRES_HOME: (not set)
POSTGRES_INCLUDE: /usr/local/pgsql/include
POSTGRES_LIB: /usr/local/pgsql/lib
OS: linux
Checking if your kit is complete...
Looks good
Warning: prerequisite Time::HiRes 0 not found.
Using DBI 1.636 (for perl 5.010001 on x86_64-linux-thread-multi) installed in /usr/local/lib64/perl5/auto/DBI/
Writing Makefile for DBD::Pg
# make
cp lib/Bundle/DBD/Pg.pm blib/lib/Bundle/DBD/Pg.pm
cp Pg.pm blib/lib/DBD/Pg.pm
/usr/bin/perl -p -e "s/~DRIVER~/Pg/g; s/^do\(/dontdo\(/" /usr/local/lib64/perl5/auto/DBI/Driver.xst > Pg.xsi
/usr/bin/perl /usr/share/perl5/ExtUtils/xsubpp  -typemap /usr/share/perl5/ExtUtils/typemap  Pg.xs > Pg.xsc && mv Pg.xsc Pg.c
gcc -c  -I/usr/local/pgsql/include -I/usr/local/lib64/perl5/auto/DBI -D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DPGLIBVERSION=90603 -DPGDEFPORT=5432 -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic  -DPERL_EXTMALLOC_DEF -Dmalloc=Perl_malloc -Dfree=Perl_mfree -Drealloc=Perl_realloc -Dcalloc=Perl_calloc -DVERSION=\"3.6.2\" -DXS_VERSION=\"3.6.2\" -fPIC "-I/usr/lib64/perl5/CORE"   Pg.c
 :
chmod 755 blib/arch/auto/DBD/Pg/Pg.so
cp Pg.bs blib/arch/auto/DBD/Pg/Pg.bs
chmod 644 blib/arch/auto/DBD/Pg/Pg.bs
Manifying blib/man3/Bundle::DBD::Pg.3pm
Manifying blib/man3/DBD::Pg.3pm
# make install
Files found in blib/arch: installing files in blib/lib into architecture dependent library tree
Installing /usr/local/lib64/perl5/auto/DBD/Pg/Pg.so
Installing /usr/local/lib64/perl5/auto/DBD/Pg/Pg.bs
Installing /usr/local/lib64/perl5/Bundle/DBD/Pg.pm
Installing /usr/local/lib64/perl5/DBD/Pg.pm
Installing /usr/local/share/man/man3/Bundle::DBD::Pg.3pm
Installing /usr/local/share/man/man3/DBD::Pg.3pm
Appending installation info to /usr/lib64/perl5/perllocal.pod

4.PostgreSQLユーザアカウント作成

PostgreSQL用のアカウントを作成します。下記のページを参考にしました。

17.1. PostgreSQLユーザアカウント

# groupadd postgres
# useradd -d /home/postgres -g postgres -s /bin/tcsh postgres
# passwd postgres
ユーザー postgres のパスワードを変更。
新しいパスワード:
よくないパスワード: 辞書の単語に基づいています
新しいパスワードを再入力してください:
passwd: 全ての認証トークンが正しく更新できました。
# chown postgres /usr/local/pgsql
# ls -l
合計 48
drwxr-xr-x. 17 root     root 4096  4月 10 11:17 2017 apache2
drwxr-xr-x.  2 root     root 4096  7月 27 10:52 2017 bin
drwxr-xr-x.  2 root     root 4096  9月 23 20:50 2011 etc
drwxr-xr-x.  2 root     root 4096  9月 23 20:50 2011 games
drwxr-xr-x.  2 root     root 4096  4月 10 10:08 2017 include
drwxr-xr-x.  4 root     root 4096  4月 10 10:08 2017 lib
drwxr-xr-x.  3 root     root 4096  4月 10 11:30 2017 lib64
drwxr-xr-x.  2 root     root 4096  9月 23 20:50 2011 libexec
drwxr-xr-x.  7 postgres root 4096  7月 27 14:10 2017 pgsql
drwxr-xr-x.  2 root     root 4096  9月 23 20:50 2011 sbin
drwxr-xr-x.  8 root     root 4096  4月 10 11:30 2017 share
drwxr-xr-x.  2 root     root 4096  9月 23 20:50 2011 src
# su - postgres
$ setenv LD_LIBRARY_PATH /usr/local/pgsql/lib
$ set path = ( /usr/local/pgsql/bin $path )

5.データベースクラスタの作成

PostgreSQLユーザでデータベースクラスタを作成します。下記のページを参考にしました。

17.2. データベースクラスタの作成

$ initdb -D /usr/local/pgsql/data
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 "ja_JP.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".
 
Data page checksums are disabled.
 
creating directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... 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:
 
    pg_ctl -D /usr/local/pgsql/data -l logfile start

6.データベースサーバの起動

データベースサーバを起動します。起動方法はいろいろあるようですが、下記のページを参考にしました。

17.3. データベースサーバの起動

$ pg_ctl -D /usr/local/pgsql/data -l logfile start
server starting

7.データベースユーザの作成

データベース用のユーザ"mtuser"を作成します。

$ createuser -P --no-adduser --no-createdb --no-createrole mtuser
Enter password for new role:
Enter it again:

8.データベースの作成

データベース"hoge"を作成します。

$ createdb --owner mtuser hoge

9.その他

ここまでの作業でMovableTypeをPostgresQLで動かせると思ったのですが、起動しても「データベースモジュールが見つかりません」となり、インストールしたはずのDBD::Pgが表示されます。

postgresql.confに下記の1行を追加し、ldconfigを実施することで解消しました。

下記のページを参考にしました。

15.6. インストール後の設定作業

# cd /etc/ld.so.conf.d
# vim postgresql.conf

下記の1行を追加。
--------------------
/usr/local/pgsql/lib
--------------------

# /sbin/ldconfig

これを行わないとMovableTypeの画面で「再試行」を実施しても状況が変わりません。

10.ノウハウ

"pg_ctl -D /usr/local/pgsql/data -l logfile start"はバックグラウンド起動しません。

バックグラウンド起動すると正常に起動されません。

上の状態に気が付かずにcreateuserすると、「could not connect to database postgres: could not connect to server」というエラーになります(下)。

$ pg_ctl -D /usr/local/pgsql/data -l logfile start 2>&1 &
[1] 6217
[postgres@mnet-host11 ~]$ createuser
createuser: could not connect to database postgres: could not connect to server: そのようなファイルやディレクトリはありません
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
[1]  + 1で終了しました pg_ctl -D /usr/local/pgsql/data -l logfile start 2 >& 1

11.参考サイト

参考サイトは下記です。ありがとうございました。

« 前の記事へ

次の記事へ »

トップページへ