You can follow the PostgreSQL installation steps from its official website and you'll be good to go but I prefer to keep only the latest version's repo i.e. pgdg18, since the first command would add pgdg repos for version 13 through 18.
# Add the pgdg repo
sudo dnf install https://download.postgresql.org/pub/repos/yum/reporpms/F-43-x86_64/pgdg-fedora-repo-latest.noarch.rpm
# Disable all pgdg repos except pgdg18
sudo dnf config-manager setopt pgdg13.enabled=0 pgdg14.enabled=0 pgdg15.enabled=0 pgdg16.enabled=0 pgdg17.enabled=0
# Verify enabled repos
sudo dnf repolist --enabled | grep pgdg # you'll see pgdg18 and pgdg-common only
# Install PostgreSQL 18 server
sudo dnf install postgresql18-server
# Initialize the database
sudo /usr/pgsql-18/bin/postgresql-18-setup initdb
# Enable and start PostgreSQL 18
sudo systemctl enable postgresql-18 --nowYou must create a new PostgreSQL user with the same name as your OS username to allow Rails application to connect to it automatically without specifying the username and password in your database.yml (see sample file below).
In your terminal, the part before @ is your username but you can also run the whoami command:
ammar@fedora:~$ whoami
ammarLogin to the psql session using postgres user
sudo -u postgres psqland create a new user with SUPERUSER privilege (replace ammar with your username)
postgres=# CREATE USER ammar WITH SUPERUSER;then list all roles to verify the new user is created.
postgres=# \duYou should also set a password for the postgres user:
postgres=# \password postgresand then quit the psql session.
postgres=# \qNote
Skip this step if your Rails application is using pg >= 1.6.0 because the gem ships with precompiled libpq.
If your Rails application requires pg <= 1.5.9, you'll going to need postgresql18-devel package to successfully install the gem:
sudo dnf install postgresql18-develbut it would still not let you install it and you'll see a similar error to below:
$ gem install pg -v 1.5.9
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
current directory: /home/ammar/.rbenv/versions/3.4.6/lib/ruby/gems/3.4.0/gems/pg-1.5.9/ext
/home/ammar/.rbenv/versions/3.4.6/bin/ruby extconf.rb
Calling libpq with GVL unlocked
checking for pg_config... no
checking for libpq per pkg-config... no
Using libpq from
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
.
.
.
extconf failed, exit code 1and that's because the gem's native extension (extconf.rb) uses pg_config, which it expects to find in /usr/bin/, while in our case, it exists in /usr/pgsql-18/bin/.
So, we'll have to provide --with-pg-config option to the gem install command with the correct path to the pg_config:
gem install pg -v 1.5.9 -- --with-pg-config=/usr/pgsql-18/bin/pg_configTo completely uninstall all postgresql versions and remove all of their data, run the following commands:
# Remove all PostgreSQL packages
sudo dnf remove 'postgresql*'
# Verify all the packages are removed
sudo rpm -qa | grep postgresql # should show nothing
# Remove all pgdg repos
sudo dnf remove pgdg-fedora-repo
# Verify all the repos are removed
sudo dnf repolist | grep pgdg # should show nothing
# Remove all the data related to pgsql
sudo rm -rf /usr/pgsql-*
sudo rm -rf /var/lib/pgsql