postgres」タグアーカイブ

jenkins pipeline内のpostgresにてDBダンプ取得

環境

CentOS Linux release 7.5.1804 (Core) DockerのHostマシン
Jenkins 2.138.1
Docker Commons Plugin
Docker Pipeline
postgres 11(公式のdocker-image latest)

やりたかったこと

他のDBサーバからデータを持って来てダンプファイルを作りたかった。

動いたJenkinsfile設定

.pgpassを任意のパス(ここではhostの/ci/app/hoge)に置き、そのパスをvオプションでマウント、さらにマウントしたファイル(ここでは/root/.pgpass)をeオプションで指定。

    
pipeline {
  agent none
  stages{
    stage('DBコピーフェーズ') {
      agent {
        docker {
          image 'postgres'
          args '-v /ci/app/hoge:/root -e PGPASSFILE=/root/.pgpass'
        }
      }
  
      steps {
        sh 'pg_dump -h ホスト名 -p 5432 -U ユーザ名 DB名 > /root/test.sql'
        sh 'echo DBコピー'
      }
    }
  }
}

動かなかった設定

.pgpassを任意のパス(ここではhostの/ci/app/hoge)に置き、そのパスをホームディレクトリとして環境変数で指定。

args '-v /ci/app/hoge:/home/ciuser -e HOME=/home/ciuser'

postgresコンテナ内の環境変数にはHOMEが反映されているが.pgpassは読み込まれなかった。
多分
getpwuidで実行ユーザ(uid1000)で環境変数ではなく/etc/passwdからホームを取得しようとする
postgresコンテナにはuid1000のユーザはいないからホームディレクトリが取得できない(echoしてみたら/で表示されてはいた)
/には.pgpassが無いからエラー
という流れと思うが調査は途中でやめた。

AWSでPSQLが入っていなかったのでyumインストール

AWSでPSQLが入っていなかったのでyumインストール。

結論としては特にはまるポイントもなく通常通りでいけた。

(青字が入力)

$yum search postgresql
Loaded plugins: priorities, update-motd, upgrade-helper
================================================================= N/S matched: postgresql ==================================================================
(中略)
postgresql92.x86_64 : PostgreSQL client programs
postgresql92-contrib.x86_64 : Extension modules distributed with PostgreSQL
postgresql92-devel.x86_64 : PostgreSQL development header files and libraries
postgresql92-docs.x86_64 : Extra documentation for PostgreSQL
postgresql92-libs.i686 : The shared libraries required for any PostgreSQL clients
postgresql92-libs.x86_64 : The shared libraries required for any PostgreSQL clients
postgresql92-plperl.x86_64 : The Perl procedural language for PostgreSQL
postgresql92-plpython26.x86_64 : The Python2 procedural language for PostgreSQL
postgresql92-plpython27.x86_64 : The Python3 procedural language for PostgreSQL
postgresql92-pltcl.x86_64 : The Tcl procedural language for PostgreSQL
postgresql92-server.x86_64 : The programs needed to create and run a PostgreSQL server
postgresql92-server-compat.x86_64 : PostgreSQL server config files for backward compatibility
postgresql92-test.x86_64 : The test suite distributed with PostgreSQL
postgresql93.x86_64 : PostgreSQL client programs
postgresql93-contrib.x86_64 : Extension modules distributed with PostgreSQL
(中略)

Name and summary matches only, use “search all” for everything.

家の環境の最新クライアント(psql)がpostgresql93.x86_64ぽいので確認

$ yum info postgresql93.x86_64
Loaded plugins: priorities, update-motd, upgrade-helper
Available Packages
Name : postgresql93
Arch : x86_64
Version : 9.3.9
Release : 1.58.amzn1
Size : 4.2 M
Repo : amzn-updates/2015.03
Summary : PostgreSQL client programs
URL : http://www.postgresql.org/
License : PostgreSQL
Description : PostgreSQL is an advanced Object-Relational database management system (DBMS).
: The base postgresql package contains the client programs that you’ll need to
: access a PostgreSQL DBMS server, as well as HTML documentation for the whole
: system. These client programs can be located on the same machine as the
: PostgreSQL server, or on a remote machine that accesses a PostgreSQL server
: over a network connection. The PostgreSQL server can be found in the
: postgresql-server sub-package.

あっていたのでインストール

$ sudo yum install postgresql93.x86_64

(中略)

Complete!

$ psql -h ホスト名 -U ユーザ名 -d DB名

つながった。