jokeirb
jokeirb

Reputation: 59

Ansible postgresql_db and postgresql_user modules

I'm trying to add database and user in an existing pgsql database (on a centOS 7.1 server). I'm using postgresql_user and postgresql_db modules (Ansible 2.2) to do it but I get the error :

fatal: [XXXXXXXXXXXXXX]: FAILED! => {"changed": false, "failed": true, "msg": "unable to connect to database: could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket \"/var/run/postgresql/.s.PGSQL.6543\"?\n"}

I'm using the following task for database creation

- name: Create database
  postgresql_db: name=db port=6543
  become: true
  become_user: postgres

For now, I have done this which is working fine but is not idempotent :

- name: Create database
  command: {{ PGBIN }}/bin/createdb db -p 6543 -w
  become: true
  become_user: postgres

I have installed as required the psycopg2 package. For information the database is not installed in the standard directory and is not using standard port 5432.

Upvotes: 3

Views: 2018

Answers (1)

James Moore
James Moore

Reputation: 399

Try either of the following:

  • pass the correct path to your postgres socket in via the login_unix_socket parameter
  • Switch to an IP connections and use login_host, login_user, and login_password but you'll need to set the postgres user password or create another superuser.

Here's what one of my tasks looks like

- name: Ensure application database is created
  postgresql_db:  name={{ postgres_app_db }}
                  encoding='UTF-8'
                  lc_collate='en_US.UTF-8'
                  lc_ctype='en_US.UTF-8'
                  state=present
                  login_host={{ postgres_client_host }}
                  login_user="{{ postgres_superuser }}"
                  login_password="{{ postgres_superuser_password }}"
                  port="{{ postgres_client_port }}"

I prefer to use IP connections because in my setup they'll work when run against the DB server or an application server.

Upvotes: 1

Related Questions