"error: could not connect to database postgres: could not connect to server" running a PostgreSQL command after starting the server

I am facing an issue with Travis executing a bash script. Here is the build log, https://travis-ci.org/github/amCap1712/pljava/jobs/688535533 . The issue is occurring at the line when chmod +x .travis/travis_setup_pljava.sh && .travis/travis_setup_pljava.sh is executed.

#!/usr/bin/env bash
if [ "$TRAVIS_OS_NAME" = "osx" ]; then
    brew services start postgresql
    /usr/local/opt/postgres/bin/createuser -s postgres
    sudo chmod 777 /home/travis

but when i execute the statements

brew services start postgresql
/usr/local/opt/postgres/bin/createuser -s postgres

separately in command line it works. Can someone help me with how to solve this issue?

The log you’ve linked to doesn’t showcase the error.

Hi! I have re run the job. You can check the logs now.

Probably ProgreSQL server is not accepting connections right after brew services start postgresql and you have to wait a tiny bit before you can create a user.

The common suggested condition to wait for is when it starts listening on the socket: