LoginSignup
4
4

More than 5 years have passed since last update.

[Ansible][PostgreSQL]AnsibleでPostgreSQLのCreate DBエラー

Posted at

概要

以前のフロジェクトで上手く動いたPostgresqlのCreate DBの部分を
新たな案件にも使うことになりました。

以前のプロジェクトよりOSもPostgreSQLもバージョンが上がりましたので、
ググッて見て設定内容が変わったか確認してみた結果、
同じOSとPostgreSQLのバージョンを利用している方の設定内容は自分の統一しました。
しかし、以下のエラーたちが発生しました。
ので、エラーメッセージ(2つ)を見て来たはずの皆様に案内差し上げます。
"systemd could not find the requested service \"'avahi-daemon'\"
"unable to connect to database: FATAL: Peer authentication failed for user \"postgres\"\n"

で、以下の対応で上手く解決できましたので、記事を投稿します。
image

サーバスペック

・CentOS 7.2

・PostgreSQL 9.4

and etc

解決方法

🌟まず "the python psycopg2 module is required"のエラーです。
エラーメッセージの内容通り、python psycopg2モジュールが必須うなので、
モジュールをインストールする処理をpostgresqlを設置するとき一緒にしましょう。

main.yml
# postgresql94のインストール
- name: install postgresql94
  yum: name={{ item }} enablerepo=pgdg94 state=present
  with_items:
  - postgresql94-server
  - postgresql94-contrib
  - python-psycopg2

🌟次はこちら、"unable to connect to database: FATAL: Peer authentication failed for user \"postgres\"\n"のエラーです。
こちらは、Ansibleの書き方が変わったらしいです。
以下のように対応しましょう。

main.yml
# データベース追加
- name: Create the RLetters PostgreSQL database
  become: yes
  become_user: postgres
  postgresql_db: db={{ dbname }}
  tags: postgresql

🌟多分原因は、OSがアップされ、提供しているパッケージが変わったからかなーと思っています。
(🔺もし原因が間違ったら、コメントお願いします!!!👏)

まとめ

とにかく、上端の対応で上手く動きます。
しかし、疑問が一つあります。

以下の内容は🔗Ansibleドキュメント
🔗「postgresql_db - Add or remove PostgreSQL databases from a remote host.」の内容一部です。

Examples

examples.yml
# Create a new database with name "acme"
- postgresql_db: name=acme
# Create a new database with name "acme" and specific encoding and locale
# settings. If a template different from "template0" is specified, encoding
# and locale settings must match those of the template.
- postgresql_db: name=acme
                 encoding='UTF-8'
                 lc_collate='de_DE.UTF-8'
                 lc_ctype='de_DE.UTF-8'
                 template='template0'

Note


This module uses psycopg2, a Python PostgreSQL database adapter. You must ensure that psycopg2 is installed on the host before using this module. If the remote host is the PostgreSQL server (which is the default case), then PostgreSQL must also be installed on the remote host. For Ubuntu-based systems, install the postgresql, libpq-dev, and python-psycopg2 packages on the remote host before using this module.

まず、Ubuntuベースではないけど、必要になったことなので、以下の内容は更新する必要があると・・・

Ubuntu-based systems, install the postgresql, libpq-dev, and python-psycopg2 packages on the remote host before using this module

そして、上の例の内容ではできない!

とにかく、2つの内容全部間違っていました。
CentOSやPostgreSQLのバージョンが上がったのでそうだかな・・・

以上です!👏

4
4
0

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
4
4