Skip to content

Instantly share code, notes, and snippets.

@vdegenne
Last active May 14, 2022 07:40
Show Gist options
  • Save vdegenne/8ac7a6048fb8000108fba1cd6b978582 to your computer and use it in GitHub Desktop.
Save vdegenne/8ac7a6048fb8000108fba1cd6b978582 to your computer and use it in GitHub Desktop.
Install PostgreSQL 12 on Red Hat distributions (tested on fedora 31)

General

First go on this page https://www.postgresql.org/download/linux/redhat/ and complete the form and follow the instructions.

Both the client and the server should be installed

sudo dnf install -y postgresql12
sudo dnf install -y postgresql12-server

Once you've done that postgresql 12 (pgsql) should be installed on your system.

Initialization and Running

At this point pgsql is installed but probably not finalized and running.
To init the database run

sudo /usr/pgsql-12/bin/postgresql-12-setup initdb

To run the server

$ sudo systemctl start postgresql-12

Use $ sudo systemctl enable postgresql-12 to run the server automatically on reboot.

First connection

If you never installed a version of pgsql on your system the first thing you want to do is to configure the user postgres.
postgres is the entry point of your database connection (the admin of admins). You use postgres to create new database or new users.
postgres has no password by default, so how do we connect ?
Luckily the default configuration of pgsql allows connection to the database using system login method.
First change postgres account password to your likings

$ sudo passwd postgres

Then connect to the user

$ su - postgres

Now we can connect to the database (using psql tool)

$ psql

We didn't provide a password because we are already authentified into the system, this method is called peer connection.
Note that because postgres is a sudo user you can also use sudo -u postgres psql as a shortcut.

Problems using the tool psql ?

For some reasons the tool psql was not recognized on my system, if you encounter the same issue here's how I fixed it

$ sudo ln -s /usr/pgsql-12/bin/psql /usr/bin/psql

this will create a symbolic link of the bin into the path and now you should be able to run the command

Changing connection method

Here you connected using the peer method, this is great for startup configuration but what you probably want is to connect to your database from a tool or an application which means you will have to change the method to allow password authentication.
If you've followed the steps above you're still connected to the database as postgres using the peer method, first change postgres password :

postgres=# alter role postgres password 'newpassword';

Now the password is changed exit psql (using \q), you're automatically disconnected from the peer postgres.

Now the password is changed for the database user postgres we can change the authentication method.
Open pg_hba.conf file

sudo vim /var/lib/pgsql/12/data/pg_hba.conf

At the bottom of the file replace all ident with md5.

Don't forget to restart the server

$ sudo systemctl restart postgresql-12

From now on you will be able to connect from anywhere using the new md5 password.

Installing GUI tool pgadmin4

psql is an excellent tool for connecting and requesting a PostgreSQL database but it's main objective is to be used in a terminal. pgadmin4 will provide an easier tool for manipulating database through a graphical interface. To install use the following :

$ sudo dnf install pgadmin4 pgadmin4-desktop-common

After installation run the program from the start menu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment