How To: PostgreSQL Install
- Chris Speed

- Jul 11, 2023
- 2 min read
Updated: Jul 21, 2023

Install PostgreSQL
sudo apt install postgresql postgresql-contrib #Ubuntu
sudo yum install postgresql postgresql-contrib #CentosStart PostgreSQL Service
sudo systemctl start postgresql.serviceSwitch to the postgres account & access the PostgreSQL
sudo -i -u postgres
psql
sudo -u postgres psqlExit PostgreSQL prompt
\qCreate a new role
createuser --interactive
sudo -u postgres createuser --interactiveOutput
Enter name of role to add: megatron
Shall the new role be a superuser? (y/n) yCreate a new database
createdb megatron
sudo -u postgres createdb megatronOpen 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)
sudo adduser megatron
sudo -u megatron psqlTo connect to a different database
psql -d postgresTo check current connection information
\conninfoOutput
You are connected to database "sammy" as user "sammy" via socket in "/var/run/postgresql" at port "5432".Open Firewall Port
sudo ufw allow 5432Edit postgresql.conf file
sudo nano /etc/postgresql/14/main/postgresql.confUncommented the following line & change the value to '*'
listen_addresses = '*'Edit pg_hba.conf file
Add an entry for the host: specify the host, the database, the authentication method, and the database user.
Note: PostgreSQL server uses SCRAM (Salted Challenge Response Authentication Mechanism) authentication
sudo nano /etc/postgresql/14/main/pg_hba.confTYPE DATABASE USER ADDRESS METHOD
host all megatron 10.0.0.167/32 scram-sha-256Restart postgreSQL service
sudo service postgresql restart
Download PostgreSQL Repository RPM
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpmInstall PosgreSQL on CentOS 7
sudo yum install -y postgresql15-serverInitialize the Database
sudo /usr/pgsql-15/bin/postgresql-15-setup initdbStart the Database
sudo systemctl start postgresql-15Enable PostgreSQL Launch on Reboot
sudo systemctl enable postgresql-15Modify the postgres user password
sudo passwd postgresOpen Firewall Port
sudo firewall-cmd --zone=public--add-port=5432/tcp --permanent
sudo firewall-cmd --reloadEdit postgres.conf file
sudo su - postgres
nano /var/lib/pgsql/15/data/postgresql.confUncommented the following line & change the value to '*'
listen_addresses = '*'Edit pg_hba.conf file
Add an entry for the host: specify the host, the database, the authentication method, and the database user.
Note: PostgreSQL server uses SCRAM (Salted Challenge Response Authentication Mechanism) authentication
nano /var/lib/pgsql/15/data/pg_hba.confTYPE DATABASE USER ADDRESS METHOD
host all all 10.0.0.167/32 scram-sha-256Restart postgreSQL service
find the service name and then reload the service
systemctl list-units|grep postgresql
service postgresql-15.service reload----------------------------------------------------------------------------------------------------
Connect to PostgreSQL with DBeaver
----------------------------------------------------------------------------------------------------
Change user password
ALTER USER username WITH PASSWORD 'new_password';References
How To Install PostgreSQL on Ubuntu 20.04 [Quickstart]. https://www.digitalocean.com/community/tutorials/how-to-install-postgresql-on-ubuntu-20-04-quickstart

Comments