# Postgresql 11.2: Cannot connect

**URL:** <https://travis-ci.community/t/postgresql-11-2-cannot-connect/2680>\
**Category:** Xenial\
**Created:** [March 17, 2019, 11:05am UTC](https://travis-ci.community/t/postgresql-11-2-cannot-connect/2680 "2019-03-17T11:05:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![keithpitty](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/keithpitty/32/1383_2.png) [@keithpitty](https://travis-ci.community/u/keithpitty)\
**Post date:** [March 17, 2019, 11:05am UTC](https://travis-ci.community/t/postgresql-11-2-cannot-connect/2680/1 "2019-03-17T11:05:53Z")

</div>

I’m having trouble getting a build working with Postgresql 11.2. As with previous attempts, the [latest build](https://travis-ci.org/keithpitty/kpdotcom/builds/507439479?utm_source=github_status&utm_medium=notification) fails when an attempt is made to run a command via `psql`.

Here’s the configuration:

```
env:
  global:
    - CC_TEST_REPORTER_ID=eca569d89e2a8d954a3e28ef958ce10e912eef6570bbdabbb28297e11948efa8
language: ruby
before_install:
  - gem update --system
  - gem install bundler
rvm:
  - 2.6.2
services:
  - postgresql
dist:
  - xenial
addons:
  postgresql: "11.2"
  apt:
    packages:
      - postgresql-11
before_script:
  - psql -c "select * from pg_settings where name = 'port';"
  - psql -c 'create database kpdotcom_test;' -U postgres
  - cp config/database.yml.travis config/database.yml
  - bundle exec rake db:schema:load
  - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
  - chmod +x ./cc-test-reporter
  - ./cc-test-reporter before-build
script:
  - bundle exec rake
after_script:
  - ./cc-test-reporter after-build -t simplecov --exit-code $TRAVIS_TEST_RESULT

```

I cannot figure out what is preventing `psql` from finding the Postgresql 11.2 service. Any help would be very much appreciated.

---

<div class="post-metadata">

**Author:** ![BanzaiMan](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/banzaiman/32/67_2.png) [@BanzaiMan](https://travis-ci.community/u/BanzaiMan)\
**Post date:** [March 18, 2019, 1:43pm UTC](https://travis-ci.community/t/postgresql-11-2-cannot-connect/2680/2 "2019-03-18T13:43:15Z")

</div>

Without looking into too much details yet, it appears to me that the package is found and configured to run on the next-available port, 5433: [https://travis-ci.org/keithpitty/kpdotcom/builds/507439479#L231](https://travis-ci.org/keithpitty/kpdotcom/builds/507439479#L231).

I would imagine that your `psql` by default will connect to port 5432, but nothing is listening on that port.

---

<div class="post-metadata">

**Author:** ![keithpitty](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/keithpitty/32/1383_2.png) [@keithpitty](https://travis-ci.community/u/keithpitty)\
**Post date:** [March 18, 2019, 9:24pm UTC](https://travis-ci.community/t/postgresql-11-2-cannot-connect/2680/3 "2019-03-18T21:24:19Z")

</div>

Hi Hiro,

Thanks for your reply. I tried your suggestion by setting `PGPORT=5433` but unfortunately `psql` is still not connecting: [https://travis-ci.org/keithpitty/kpdotcom/builds/508095041](https://travis-ci.org/keithpitty/kpdotcom/builds/508095041).

There must be something else I’m missing.

Keith

---

<div class="post-metadata">

**Author:** ![keithpitty](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/keithpitty/32/1383_2.png) [@keithpitty](https://travis-ci.community/u/keithpitty)\
**Post date:** [March 18, 2019, 9:28pm UTC](https://travis-ci.community/t/postgresql-11-2-cannot-connect/2680/4 "2019-03-18T21:28:33Z")

</div>

Actually, I see that `psql` is still looking for the PostgreSQL server on port 5432 so setting the `PGPORT` environment variable has not had any effect. 🤔

---

<div class="post-metadata">

**Author:** ![jondoesntgit](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/jondoesntgit/32/1434_2.png) [@jondoesntgit](https://travis-ci.community/u/jondoesntgit)\
**Post date:** [March 22, 2019, 11:48pm UTC](https://travis-ci.community/t/postgresql-11-2-cannot-connect/2680/5 "2019-03-22T23:48:42Z")

</div>

I had this problem too. Here’s the config file that fixed it for me:

This script:

1. Shuts down all 9.\* postgreSQL databases
2. installs 11.2 (at the time of this writing)
3. copies the authentication information from the old 9.6 configuration
4. creates a role called “travis”

```auto
language: ruby
rvm: 2.6.2
before_install:
  - sudo apt-get update
  - sudo apt-get --yes remove postgresql\*
  - sudo apt-get install -y postgresql-11 postgresql-client-11
  - sudo cp /etc/postgresql/{9.6,11}/main/pg_hba.conf
  - sudo service postgresql restart 11  
before_script:
  - psql --version
  - psql -c 'CREATE DATABASE {{your database name here}};' -U postgres
  - psql -c 'CREATE ROLE travis SUPERUSER LOGIN CREATEDB;' -U postgres
  - cp config/database.yml.travis config/database.yml
script: bundle exec rake spec
services:
  - postgresql
addons:
  postgresql: "11.2"

```

---

<div class="post-metadata">

**Author:** ![keithpitty](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/keithpitty/32/1383_2.png) [@keithpitty](https://travis-ci.community/u/keithpitty)\
**Post date:** [March 26, 2019, 11:43am UTC](https://travis-ci.community/t/postgresql-11-2-cannot-connect/2680/6 "2019-03-26T11:43:04Z")

</div>

Thanks very much for your response, @jondoesntgit. Much appreciated. And apologies for the delay in replying. You’re suggestion worked for me!

---

<div class="post-metadata">

**Author:** ![jondoesntgit](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/jondoesntgit/32/1434_2.png) [@jondoesntgit](https://travis-ci.community/u/jondoesntgit)\
**Post date:** [March 26, 2019, 3:27pm UTC](https://travis-ci.community/t/postgresql-11-2-cannot-connect/2680/7 "2019-03-26T15:27:16Z")

</div>

Happy to help.

---

<div class="post-metadata">

**Author:** ![mickeytgl](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/mickeytgl/32/1879_2.png) [@mickeytgl](https://travis-ci.community/u/mickeytgl)\
**Post date:** [May 29, 2019, 3:06pm UTC](https://travis-ci.community/t/postgresql-11-2-cannot-connect/2680/8 "2019-05-29T15:06:45Z")

</div>

I was stuck on this issue for like 3 hours, and your comment solved it, thank you so much @jondoesntgit

---

<div class="post-metadata">

**Author:** ![stevencch99](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@stevencch99](https://travis-ci.community/u/stevencch99)\
**Post date:** [July 21, 2019, 2:48pm UTC](https://travis-ci.community/t/postgresql-11-2-cannot-connect/2680/9 "2019-07-21T14:48:32Z")

</div>

That’s really helpful! thanks for share.

---

<div class="post-metadata">

**Author:** ![dominic](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/dominic/32/14_2.png) [@dominic](https://travis-ci.community/u/dominic)\
**Post date:** [July 26, 2019, 3:31pm UTC](https://travis-ci.community/t/postgresql-11-2-cannot-connect/2680/10 "2019-07-26T15:31:02Z")

</div>


