How to setup PostgreSQL database for Rails development on Fedora 35

How to setup PostgreSQL database for Rails development on Fedora 35

Not a long time ago I switched my work linux distribution from Ubuntu to Fedora. As a Ruby on Rails developer, I need to run PostgreSQL database locally to develop and test some of my projects. But confingure PostgreSQL on Fedora showed to be a problem.

On Ubuntu the installation was pretty straightforward. I followed instructions on PostgreSQL website, setup password for postgres user and I was able to connect from my RoR application.

On Fedora however, after following same process and setting password, I was unable to connect to my database and I was getting error like:

psql: error: FATAL: Ident authentication failed for user "postgres".

Steps that finally worked for me

  1. Install PostgreSQL with this command:
sudo dnf install postgresql-server postgresql-devel
  1. Don't forget to init database and enable automatic startup:
postgresql-setup --initdb
systemctl enable postgresql.service
systemctl start postgresql.service
  1. Setup password for posgtres user:
sudo -u postgres psql
\password postgres 
# enter new password
  1. Now when you try to connect to database with your newly created password:
psql -U postgres -W -hlocalhost

you get above-mentioned error psql: error: FATAL: Ident authentication failed for user "postgres".

That's weird right because on Ubuntu I was able to connect after these steps. So how do we fix this?

  1. Open sudo nano /var/lib/pgsql/data/pg_hba.conf and change it as following:
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident
  1. Save the file and restart postgresql service:
sudo service postgresql restart
  1. Now you should be able to connect, so try again:
psql -U postgres -W -hlocalhost
# enter password

Now It should work :)

Then in your rails app config/database.yml, just set:

development:
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: postgres
  password: your_postgres_password
  host: localhost

I hope this helps, because at first I struggled a lot, to get this working. To write an article, I was inspired by Reddit thread, where folks switching from Ubuntu and Mac OS had same problem.