ngx_mrubyからPostgreSQLサーバにアクセス（mruby-pgを組み込む)

# 現状

- ngx_mrubyに組み込むところは解決！！
- 組込んで、プログラム書いてないので、本当に問題ないかの確認がまだです。。。<br/>（なかなか前に進められないので、先に原因等の記載をします）

# 使用ライブラリ

https://github.com/authorNari/mruby-pg

# 環境

OS: CentOS Linux 6.7 x86_64
PostgerSQL: ソースビルド 9.4.5 (prefix =/usr/local/pgsql/9.4.5)

# 問題

ldconfig等で、パス通っているのに、ビルドができなかった。

```
/usr/bin/ld: cannot find -lpq
collect2: ld はステータス 1 で終了しました
/usr/bin/ld: cannot find -lpq
collect2: ld はステータス 1 で終了しました
rake aborted!
```

# 原因

原因は、/usr/lib64/にlibmysql関係のファイルが存在しないこと

参照：「/usr/bin/ld: cannot find -lmysqlclient」が出たときにやったこと
http://qiita.com/HirokiTakaba/items/0ec56590aee83aa371d4
(mysqlでも問題が起きる感じですね。。。）

なぜ、ldconfigでパスまで通ってるのに、これが必要なのかは不明

# ビルド方法(解決方法含む）

https://github.com/matsumoto-r/ngx_mruby/releases

から 1.7.12を取得して、 build_config.rbに以下を追加

```
conf.gem :github => 'authorNari/mruby-pg'
```

ビルド実行前には、

```
export PG_CONFIG=/usr/local/pgsql/9.4.5/bin/pg_config
export PATH=$PATH:/usr/local/pgsql/9.4.5/bin
```

を実行して、pg_configが使用できるようにしています。

```
find `pg_config --libdir` -type f -print0 |xargs -0 -I{} ln -snf {} /usr/lib64/
```

にて、`/usr/lib64/` にシンボリックリンクを作成します。

これで、ビルドすると、完成。

nginxからmrubyを介して、ダイレクトにアクセスできる準備が整った！！（ハズ。。。）

# ngx_mrubyで追加モジュールを導入する際の注意。

最初、git cloneしたものではなく、リリースされたtar.gzをつかっていました。
上記状態で、ビルドをすると、

```
fatal: Not a git repository (or any of the parent directories): .git
```

というエラーがでます。

以下がエラーの内容全部。

```
CC    build/mrbgems/mruby-pg/src/mrb_pg.c -> build/host/mrbgems/mruby-pg/src/mrb_pg.o
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c: In function 'pgresult_get':
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:82: warning: dereferencing type-punned pointer will break strict-aliasing rules
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c: In function 'pgresult_check':
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:178: warning: dereferencing type-punned pointer will break strict-aliasing rules
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c: In function 'pgresult_size':
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:82: warning: dereferencing pointer 'result.19' does break strict-aliasing rules
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:82: note: initialized from here
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c: In function 'pgresult_aref':
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:82: warning: dereferencing pointer 'result.19' does break strict-aliasing rules
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:82: note: initialized from here
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c: In function 'pgresult_clear':
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:82: warning: dereferencing pointer 'result.19' does break strict-aliasing rules
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:82: note: initialized from here
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c: In function 'pgresult_check':
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:178: warning: dereferencing pointer 'result.24' does break strict-aliasing rules
/root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:178: note: initialized from here
      MRBC build/mrbgems/mruby-pg/mrblib/mrb_pg.rb
CC    build/host/mrbgems/mruby-pg/gem_init.c -> build/host/mrbgems/mruby-pg/gem_init.o
CC    build/host/mrbgems/gem_init.c -> build/host/mrbgems/gem_init.o
AR    build/host/lib/libmruby.a
ar: creating /root/rpmbuild/BUILD/nginx-1.10.0/ngx_mruby_pg-1.17.2/mruby/build/host/lib/libmruby.a
CC    mrbgems/mruby-bin-mruby/tools/mruby/mruby.c -> build/host/mrbgems/mruby-bin-mruby/tools/mruby/mruby.o
LD    build/host/bin/mruby
CC    mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c -> build/host/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.o
LD    build/host/bin/mruby-strip
CC    mrbgems/mruby-bin-mirb/tools/mirb/mirb.c -> build/host/mrbgems/mruby-bin-mirb/tools/mirb/mirb.o
LD    build/host/bin/mirb

Build summary:

================================================
      Config Name: host
 Output Directory: build/host
         Binaries: mruby-config, mrbc
    Included Gems:
             mruby-sprintf - standard Kernel#sprintf method
             mruby-print - standard print/puts/p
             mruby-objectspace - ObjectSpace class
             mruby-compiler - mruby compiler library
             mruby-bin-mruby - mruby command
               - Binaries: mruby
             mruby-proc-ext - Proc class extension
             mruby-object-ext - Object class extension
             mruby-bin-mrbc - mruby compiler executable
             mruby-fiber - Fiber class
             mruby-enum-ext - Enumerable module extension
             mruby-enumerator - Enumerator class
             mruby-random - Random class
             mruby-array-ext - Array class extension
             mruby-enum-lazy - Enumerable::Lazy class
             mruby-eval - standard Kernel#eval method
             mruby-symbol-ext - Symbol class extension
             mruby-range-ext - Range class extension
             mruby-math - standard Math module
             mruby-toplevel-ext - toplevel object (main) methods extension
             mruby-string-ext - String class extension
             mruby-kernel-ext - Kernel module extension
             mruby-numeric-ext - Numeric class extension
             mruby-hash-ext - Hash class extension
             mruby-error - extensional error handling
             mruby-time - standard Time class
             mruby-struct - standard Struct class
             mruby-bin-strip - irep dump debug section remover command
               - Binaries: mruby-strip
             mruby-exit - Kernel#exit method
             mruby-bin-mirb - mirb command
               - Binaries: mirb
             mruby-io
             mruby-env
             mruby-dir
             mruby-digest
             mruby-process
             mruby-pack
             mruby-socket
             mruby-json
             mruby-onig-regexp
             mruby-sleep - 0.0.1
             mruby-redis - 0.0.1
             mruby-vedis - 0.0.1
             mruby-userdata
             mruby-uname - 0.0.1 - system uname binding
             mruby-mutex
             mruby-cache
             mruby-ngx-mruby-ext
             rack-based-api
             mruby-pg - postgresql mruby binding
================================================

fatal: Not a git repository (or any of the parent directories): .git
make: *** [build_mruby] Error 128
エラー: /var/tmp/rpm-tmp.fcKudg の不正な終了ステータス (%prep)
```

上記は、git管理化じゃないので、問題みたいなので、git cloneしたものであれば、発生しません。
(rpm作成して、バージョン管理したかったので、リリース使いたかったんですが、とりあえず、今回はビルド通したかったので。。。）

尚、インストール手順はいくつかあって、例えばローカルのmrubyを使いたい場合は
`configureオプション --with-mruby-root=DIR `で渡すことはできます。 と教えてもらいました。
（まだ未調査です。。。）

ちなみに安易に、

`git init `すればいけるだろう！！っておもってやると、以下のようなエラーが出ます。

```
error: pathspec 'mruby/build_config.rb' did not match any file(s) known to git.
```

# 調査記録

以下は、解決までに至った調査記録。ただのメモ。。。

## エラー内容

```

... (上部は量が多いので、省略）

CC    build/mrbgems/mruby-pg/src/mrb_pg.c -> build/host/mrbgems/mruby-pg/src/mrb_pg.o
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c: In function ‘pgresult_get’:
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:82: 警告: dereferencing type-punned pointer will break strict-aliasing rules
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c: In function ‘pgresult_check’:
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:178: 警告: dereferencing type-punned pointer will break strict-aliasing rules
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c: In function ‘pgresult_size’:
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:82: 警告: dereferencing pointer ‘result.19’ does break strict-aliasing rules
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:82: note: initialized from here
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c: In function ‘pgresult_aref’:
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:82: 警告: dereferencing pointer ‘result.19’ does break strict-aliasing rules
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:82: note: initialized from here
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c: In function ‘pgresult_clear’:
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:82: 警告: dereferencing pointer ‘result.19’ does break strict-aliasing rules
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:82: note: initialized from here
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c: In function ‘pgresult_check’:
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:178: 警告: dereferencing pointer ‘result.24’ does break strict-aliasing rules
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/mrbgems/mruby-pg/src/mrb_pg.c:178: note: initialized from here
      MRBC build/mrbgems/mruby-pg/mrblib/mrb_pg.rb
CC    build/host/mrbgems/mruby-pg/gem_init.c -> build/host/mrbgems/mruby-pg/gem_init.o
CC    build/host/mrbgems/gem_init.c -> build/host/mrbgems/gem_init.o
AR    build/host/lib/libmruby.a
ar: creating /usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/host/lib/libmruby.a
CC    mrbgems/mruby-bin-mruby/tools/mruby/mruby.c -> build/host/mrbgems/mruby-bin-mruby/tools/mruby/mruby.o
LD    build/host/bin/mruby
/usr/bin/ld: cannot find -lpq
collect2: ld はステータス 1 で終了しました
/usr/bin/ld: cannot find -lpq
collect2: ld はステータス 1 で終了しました
rake aborted!
Command failed with status (1): [gcc  -o "/usr/local/src/ngx_mruby_pg-1.17....]
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/tasks/mruby_build_commands.rake:31:in `_run'
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/tasks/mruby_build_commands.rake:36:in `rescue in _run'
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/tasks/mruby_build_commands.rake:32:in `_run'
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/tasks/mruby_build_commands.rake:182:in `run'
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/Rakefile:70:in `block (4 levels) in <top (required)>'
Command failed with status (1): ["gcc"  -o "/usr/local/src/ngx_mruby_pg-1.1...]
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/tasks/mruby_build_commands.rake:33:in `_run'
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/tasks/mruby_build_commands.rake:182:in `run'
/usr/local/src/ngx_mruby_pg-1.17.2/mruby/Rakefile:70:in `block (4 levels) in <top (required)>'
Tasks: TOP => default => all => /usr/local/src/ngx_mruby_pg-1.17.2/mruby/bin/mruby => /usr/local/src/ngx_mruby_pg-1.17.2/mruby/build/host/bin/mruby
(See full trace by running task with --trace)
make: *** [build_mruby] エラー 1
```

## 試したこと

mruby-pg側のmrbgem.rakeのGEM specｗいじったりとかしましたが改善されず。。。

とくに、mrbgem.rakeは、いろいろ試行錯誤して、変更して対応していますが、対応されず。。。

### mrbgem.rake

```rb
MRuby::Gem::Specification.new('mruby-pg') do |spec|
  spec.license = 'MIT'
  spec.authors = 'Narihiro Nakamura'
  spec.summary = 'postgresql mruby binding'

  # for debug
  # spec.cc.flags = ["-g3", "-std=gnu99", "-O0", "-Wall", "-Werror-implicit-function-declaration", "-Wdeclaration-after-statement"]

  spec.cc.include_paths << `pg_config --includedir`.chomp
  spec.linker.library_paths << `pg_config --libdir`.chomp
  spec.linker.libraries << 'pq'
end
``` 


## その他、

ソースコンパイルでなにかおかしいのかと思って、yum で、 postgres94-libsや、develを入れても同様の結果でした。

### ldconfig

```
# ldconfig -p |grep pq
	libpqwalreceiver.so (libc6,x86-64) => /usr/local/pgsql/9.4.5/lib/libpqwalreceiver.so
	libpq.so.5 (libc6,x86-64) => /usr/local/pgsql/9.4.5/lib/libpq.so.5
	libpq.so (libc6,x86-64) => /usr/local/pgsql/9.4.5/lib/libpq.so
	libipq.so.0 (libc6,x86-64) => /lib64/libipq.so.0
```

ちゃんとパスも通っている。。。