Alfonso Embid-Desmet
Alfonso Embid-Desmet

Reputation: 3632

Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"

I am provisioning a 'Precise64' Ubuntu machine via Ansible, I am encountering an error when ensuring that the database is in fact created. I am using most of https://github.com/jcalazan/ansible-django-stack

- name: Install PostgreSQL
  apt: name={{ item }} update_cache={{ update_apt_cache }} state=installed
  with_items:
    - postgresql
    - postgresql-contrib
    - python-psycopg2
  tags: packages

- name: Ensure the PostgreSQL service is running
  service: name=postgresql state=started enabled=yes

- name: Ensure database is created
  sudo_user: postgres
  postgresql_db: name={{ db_name }}
                 encoding='UTF-8'
                 lc_collate='en_US.UTF-8'
                 lc_ctype='en_US.UTF-8'
                 template='template0'
                 state=present

However, when arriving to the last task, I get this error:

failed: [default] => {"failed": true, "playbook": "vagrant.yml", "role": "db", "task": "Ensure database is created"} msg: unable to connect to database: 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"?

FATAL: all hosts have already failed -- aborting

Upvotes: 2

Views: 3242

Answers (3)

alfetopito
alfetopito

Reputation: 1503

The answer by @damgad indeed works for me, but since I'm lazy, I don't wanna have to export all of those env vars every time I need to setup Postgresql.

I could put it into my .bash_rc or similar, but I also don't want to have it defined account wide, since the point here is because I have a different locale.

So, using part of the initial answer by @alfonso-perez, I ended up using this two extra tasks, and haven't had to export anything again:

- name: Install Language packages
  apt: name=language-pack-en update_cache={{ update_apt_cache }} force=yes state=latest
  tags: packages

- name: Locale (is a b****)
  locale_gen: name={{ item }} state=present
  with_items:
    - en_US
    - en_US.UTF-8
    - pt_BR.UTF-8
    - de_DE.UTF-8

- name: Install PostgreSQL
  apt: name={{ item }} update_cache={{ update_apt_cache }} state=installed
  with_items:
    - postgresql-9.3
    - postgresql-contrib-9.3
    - postgresql-server-dev-9.3
    - python-psycopg2
  tags: packages

Upvotes: 4

damgad
damgad

Reputation: 1446

I had the same issue with ansible and vagrant on my Ubuntu 14.04 LTS. Alfonso's answer didn't solve the problem.

I realised that vagrant copies localisation settings from my system (which were not en_US) to the virtual one. That caused database creation problem.

Running following shell code before vagrant up fixed the problem:

export LANGUAGE=en_US
export LC_CTYPE="en_US.UTF-8"
export LC_NUMERIC="en_US.UTF-8"
export LC_TIME="en_US.UTF-8"
export LC_COLLATE="en_US.UTF-8"
export LC_MONETARY="en_US.UTF-8"
export LC_MESSAGES="en_US.UTF-8"
export LC_PAPER="en_US.UTF-8"
export LC_NAME="en_US.UTF-8"
export LC_ADDRESS="en_US.UTF-8"
export LC_TELEPHONE="en_US.UTF-8"
export LC_MEASUREMENT="en_US.UTF-8"
export LC_IDENTIFICATION="en_US.UTF-8"

That temporarily changes localisation settings so that en_US will be copied to virtual system.

Upvotes: 5

Alfonso Embid-Desmet
Alfonso Embid-Desmet

Reputation: 3632

after some debugging, I got it working by installing the language pack so the postgres daemon doesn't complain about the Locale when starting

- name: Install Language packages
  apt: name=language-pack-en update_cache={{ update_apt_cache}} force=yes state=latest
  tags: packages

Upvotes: 1

Related Questions