Search code examples
postgresqltravis-ci

Travis-CI and PostgreSQL: psql: error: could not connect to server: No such file or directory


I'm trying to get a PostgreSQL 12 Database for my Travis-CI build. I've been trying for hours. I've looked at the other threads on Travis-CI Community and on StackOverFlow. But I've made no progress and it keeps failing.

I'm using dist: focal so it has PostgreSQL 12.2. So I don't need to mess with addons: postgres: '12' and apt: ...
The psql --version command works, but the psql -c 'create database ... command does not.

If I remove the psql -c 'create database ... command, my application fails to start due to the missing Database, with

Caused by: org.postgresql.util.PSQLException at ConnectionFactoryImpl.java:303
Caused by: java.net.ConnectException at Net.java:-2

Yes, I've read the Travis Database Setup Documentation.
The link to the Travis build is: lukegjpotter/pokemon-team-building-tools/jobs/480144598.
Please Help.

The relevant lines in my travis.yml file is as follows:

language: java
os: linux
dist: focal
jdk:  oraclejdk13
services:
  - postgresql
before_script:
  - cp config/database.yml.travis config/database.yml
  - psql --version
  - psql -c 'create database travis_ci_test;' -U postgres
env: DATABASE_URL=postgres://postgres:@localhost:5432/travis_ci_test

The relevant lines in my build output is as follows:

$ psql --version
psql (PostgreSQL) 12.5 (Ubuntu 12.5-0ubuntu0.20.04.1)

$ psql -c 'create database travis_ci_test;' -U postgres
psql: error: 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"?
The command "psql -c 'create database travis_ci_test;' -U postgres" failed and exited with 2 during .

Regards, Luke


Solution

  • The issues is the Port Number in the error message.

    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
    

    On some distros and postgres versions, Postgres runs on port 5432.
    On dist: focal and services: postgresql the port is 5433. The psql command uses port 5432.
    To solve this, you need to add the -p 5433 to the psql command:

    $ psql -c 'CREATE DATABASE travis_ci_test;' -U postgres -p 5433
    

    This will then give a new error;

    psql: error: FATAL:  Peer authentication failed for user "postgres"
    The command "psql -c 'CREATE DATABASE travis_ci_test;' -U postgres -p 5433" failed and exited with 2 during .
    

    But that is an error for another day.