Prerequisite
You will need one (physical or virtual) machine with CentOS/RHEL 8 minimal installed having sudo non-root user privileges.Set Timezone
It is important to set the correct timezone on your fresh installed server before proceeding to install database:You can find the correct timeones with below command:
Type below command to set the time zone, replace Asia/Karachi with yours:
Adding PostgreSQL/EPEL Repo
Now, we will add postgresql official repository to get the most latest version of postgresql database:We will also add EPEL repository:
Installing PostgreSQL Database
At the time of writing this tutorial, the most latest release of postgresql was 12. You can install the most latest release of postgresql database with below commands:Now that the postgresql server is installed, you will perform some basic steps to prepare a new database cluster.
Creating PostgreSQL Database Cluster
A database cluster is a collection of databases that are managed by a single server instance. Creating a database cluster consists of creating the directories in which the database data will be placed, generating the shared catalog tables, and creating the template and postgres databases.You have to create a new PostgreSQL database cluster before you can use your Postgres database.
You will see the output similar to below:
Initializing database ... OK
Now, start the postgresql database service with below command:
Update firewall rules to allow postgresql if you need to access database from remote clients.
Now that PostgreSQL is up and running, you will go over roles to learn how Postgres works and how it is different from similar database management systems you may have used in the past.
Creating PostgreSQL Roles and Databases
By default, Postgres uses a concept called roles to handle in authentication and authorization. These are, in some ways, similar to regular Unix-style accounts, but Postgres does not distinguish between users and groups and instead prefers the more flexible term role.Upon installation, Postgres is set up to use ident authentication, meaning that it associates Postgres roles with a matching Unix/Linux system account. If a role exists within Postgres, a Unix/Linux username with the same name is able to sign in as that role.
The installation procedure created a user account called postgres that is associated with the default Postgres role. In order to use Postgres, you can log in to that account.
There are several ways to use this account to access Postgres.
Switch over to the postgres account on your server by typing:
Access a Postgres prompt by typing:
This will log you into the PostgreSQL prompt, and from here you can interact with the database management system right away.
You can log out from the PostgreSQL prompt by typing:
This will bring you back to the original Linux sudo prompt.
Accessing a Postgres Prompt Without Switching Accounts
For instance, in the earlier example, you were instructed to get to the Postgres prompt by first switching to the postgres user and then running psql to open the Postgres prompt. You could do this in one step by running the single command psql as the postgres user with sudo, like below:This will log you directly into Postgres prompt.
Creating a New Role in Postgres
With fresh installation, you just have the postgres role configured within the database. You can create new roles using the createrole command. The --interactive flag will prompt you for the name of the new role and also ask whether it should have superuser permissions.To create a user on the default database with prompting additional attributes:
To delete a user from the default database with prompting additional attributes:
Creating a New Database in PostgreSQL
If the user you created in the last section is called peter, that role will attempt to connect to a database which is also called peter by default.You can create the appropriate database with the createdb command.
Now that you’ve created a new database, you will log in to it with your new role.
Opening a Postgres Prompt with the New Role
To log in with ident based authentication, you’ll need a Linux user with the same name as your Postgres role and database.If you don’t have a matching Linux user available, you can create one with the adduser command. You will have to do this from your non-root account with sudo privileges (meaning, not logged in as the postgres user):
Once this new account is available, you can switch over and connect to the default database by typing:
If you want your user to connect to its own database, you can do so by specifying the database like this:
No comments: