I've successfully installed postgres on my Ubuntu machine. I am running a script for a website I am working on, here is the relevant code:

apt-get -y install postgresql libpq-dev
su postgres <<EOF
    createuser app
    createdb ultimaterehab
PG_HBA=$(su postgres -c "psql -t -P format=unaligned -c 'SHOW hba_file'")

When executed this script gives the error:

createuser: could not connect to database postgres: FATAL:  no pg_hba.conf entry for host "[local]", user "postgres", database "postgres", SSL off
createdb: could not connect to database template1: FATAL:  no pg_hba.conf entry for host "[local]", user "postgres", database "template1", SSL off

and when I try running psql I get a similar error:

psql: FATAL:  no pg_hba.conf entry for host "[local]", user "robin", database "robin", SSL off

I have tried editing the pg_hba.conf file to add the hosts but I keep getting the same error.

1 Answer 1


PG_HBA=$(su postgres -c "psql -t -P format=unaligned -c 'SHOW hba_file'")

That seems potentially problematic.

  • That line above shows createdb ultimaterehab. That line doesn't however connect to the ultimaterehab database. It connects to the default database.
  • Debian/Ubuntu already does the setting up for the right pg_hba. You don't have to do anything whatsoever. If you absolutely need the location of the pg_hba file, which you do not need, you'd be better of working with the system utility pg_conftool to get that location: pg_conftool show hba_file.

Moreover, ubuntu doesn't use su, it uses sudo.

sudo -u postgres createuser app
sudo -u postgres createdb ultimaterehab

And you're done.

  • Thanks for the response, I get the same error when I do sudo (and su does work in Ubuntu), I still get errors when I run psql. Oct 23, 2017 at 20:03
  • @RobinReborn in Ubuntu root is locked by default and so is the postgres user. The only way that would work is if you manually unlocked the postgres user (don't do that, it's another vector for attack). Oct 23, 2017 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.