狂ったお茶会のlog

後で起きる自分のためのメモ

PostgreSQLのソースをサーバにインストールする

インストールする

初っ端から蛇足だが、どっからDLしていいかしばらくわからなかったので書いておくと
http://www.postgresql.org/ のDLページ、左端のメニューのSourceを押すと出て来る
f:id:dormouse666:20151014015756p:plain

今回は9.4.4を入れます

※root状態でやる

cd /usr/local/src    //場所移動
curl -O https://ftp.postgresql.org/pub/source/v9.4.4/postgresql-9.4.4.tar.gz   //ソースを取得
tar xvzf postgresql-9.4.4.tar.gz   //展開
cd postgresql-9.4.4    //できたフォルダに移動

この後./configureをしたが以下のエラー出た

configure: WARNING:
*** Without Bison you will not be able to build PostgreSQL from Git nor
*** change any of the parser definition files.  You can obtain Bison from
*** a GNU mirror site.  (If you are using the official distribution of
*** PostgreSQL then you do not need to worry about this, because the Bison
*** output is pre-generated.)
checking for flex... no
configure: WARNING:
*** Without Flex you will not be able to build PostgreSQL from Git nor
*** change any of the scanner definition files.  You can obtain Flex from
*** a GNU mirror site.  (If you are using the official distribution of
*** PostgreSQL then you do not need to worry about this because the Flex
*** output is pre-generated.)
checking for perl... no
configure: WARNING:
*** Without Perl you will not be able to build PostgreSQL from Git.
*** You can obtain Perl from any CPAN mirror site.
*** (If you are using the official distribution of PostgreSQL then you do not
*** need to worry about this, because the Perl output is pre-generated.)
checking for main in -lm... yes
checking for library containing setproctitle... no
checking for library containing dlopen... -ldl
checking for library containing socket... none required
checking for library containing shl_load... no
checking for library containing getopt_long... none required
checking for library containing crypt... -lcrypt
checking for library containing shm_open... -lrt
checking for library containing shm_unlink... none required
checking for library containing fdatasync... none required
checking for library containing gethostbyname_r... none required
checking for library containing shmget... none required
checking for library containing readline... no
configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.

bisonとflexperlreadlineがない?
WARNINGは放置してとりあえずreadlineを入れろとの天のお告げがあったので以下インストール

yum install readline readline-devel

上記した後、以下

./configure
make
make install

ビャーッと色々出た最後にPostgreSQL installation complete.となって、インストール完了

ここからインストール後

mkdir /usr/local/pgsql/data   //格納先?を作る
chown postgres:postgres /usr/local/pgsql/data   //格納先の権限移譲

postgresユーザ作成
※セキュリティ上、rootだとpostgresqlは起動できない

useradd postgres   //ユーザ作成
passwd postgres   //パスワード設定
su - postgres   //ユーザ切り替え

posgresユーザで以下をする

cd /usr/local/pgsql/bin   //場所移動
./initdb --encoding=UTF8 --no-locale -D /usr/local/pgsql/data   //initdbする。-Dでディレクトリ指定しないとエラーだった

ビャーッと出た後に

Success. You can now start the database server using:

    ./postgres -D /usr/local/pgsql/data
or
    ./pg_ctl -D /usr/local/pgsql/data -l logfile start

と出て完了

起動したい

上記のコマンドで起動できるようだが

./pg_ctl -D /usr/local/pgsql/data -l logfile start

↑をやってみたけど

$ sh: logfile: Permission denied

とか出てしまった、権限がない?

絶対パスを指定してみる
※以下を拝見した
MacPorts で PostgreSQL をインストールする - make world

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

動いたっぽい?

確認(1)

$ ps awx | grep postgres
11502 pts/0    S      0:00 su - postgres
11558 pts/1    S      0:00 su - postgres
11620 pts/0    S      0:00 /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
11622 ?        Ss     0:00 postgres: checkpointer process                        
11623 ?        Ss     0:00 postgres: writer process                              
11624 ?        Ss     0:00 postgres: wal writer process                          
11625 ?        Ss     0:00 postgres: autovacuum launcher process                 
11626 ?        Ss     0:00 postgres: stats collector process                     
11628 pts/1    S+     0:00 grep postgres

確認(2)

$ ./psql -l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     | 
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
(3 rows)

確認方法がこれで合ってるのかよくわかってないが

もう1つのコマンドの↓もやってみた

./postgres -D /usr/local/pgsql/data

以下になって止まる、上記と同じくpsした感じ動いてるっぽい気配

$ ./postgres -D /usr/local/pgsql/data
LOG:  database system was shut down at 2015-10-04 15:53:16 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

スキーマつくる

testというスキーマ作成してみた

$ ./createdb test
$ ./psql test
psql (9.4.4)
Type "help" for help.

test=# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     | 
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 test      | postgres | UTF8     | C       | C     | 
(4 rows)

ここまで、何もPATHを通してないので後で通す
一旦以上

追記

PATH通した
PATHを通す - 狂ったお茶会のlog

参考

ここらへんを見ました

http://www.postgresql.jp/document/9.4/html/installation.html

CentOS6.x - postgresインストール(ソースから) - Qiita

PostGISを使ってみる - shohu33's diary

desknet's NEO (Linux+PostgreSQL 9.2) インストールガイド|desknet's NEO

使えば分かるPostgreSQL運用&チューニング(1):PostgreSQL導入から接続まで (1/3) - @IT