ANXS / postgresql

Fairly full featured Ansible role for Postgresql.
http://anxs.io/
MIT License
849 stars 573 forks source link

PSQL tasks fails when `postgresql_port` != 5432 (default value) #445

Closed lrk closed 4 years ago

lrk commented 4 years ago

When using postgresql_port with custom value ( != 5432), tasks relying on psql fails with message:

psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

It seem that psql client don't get the correct socket name based on the actual configured port, which is 5612 in my case:

>ls -al /var/run/postgresql/
total 8
drwxrwsr-x.  2 postgres postgres 100 Sep  2 12:48 .
drwxr-xr-x. 24 root     root     820 Sep  2 13:09 ..
-rw-r--r--.  1 postgres postgres   6 Sep  2 12:48 10-data.pid
srwxrwxrwx.  1 postgres postgres   0 Sep  2 12:48 .s.PGSQL.5612
-rw-------.  1 postgres postgres  66 Sep  2 12:48 .s.PGSQL.5612.lock

I think a simple fix would be to add PGPORT env var to each task relaying on psql client:

for example:

# Check database version
- name: PostgreSQL | Check database version
  shell: >
    psql --quiet --tuples-only --command="select substring(version(),'[^\s]+\s+[^\s]+');" | sed 's/^ //'
  become: yes
  become_user: "{{ postgresql_service_user }}"
  changed_when: false
  failed_when: postgresql_database_version.stdout == ""
  register: postgresql_database_version
  check_mode: no  
  environment:
    - PGPORT: "{{ postgresql_port }}"

Temporary fix, add environment PGPORT to the playbook step calling the postgresl role:

- name: "Provision Postgresql"
  hosts: dbs
  become: true
  roles:
    - anxs.postgresql
  environment:
    - PGPORT: "{{ postgresql_port }}"

Envs:

lrk commented 4 years ago

i create a PR for that: #446