1. Install from postgresql.org
$ sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
$ sudo yum install -y postgresql14-server
$ rpm -qa | grep postgres
postgresql14-libs-14.9-2PGDG.rhel7.x86_64
postgresql14-14.9-2PGDG.rhel7.x86_64
postgresql14-server-14.9-2PGDG.rhel7.x86_64
$ sudo systemctl enable postgresql-14
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-14.service to /usr/lib/systemd/system/postgresql-14.service.
2. Start postgres
$ sudo systemctl start postgresql-14
$ netstat -ltpn | grep postmaster
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 15087/postmaster
tcp6 0 0 ::1:5432
3. Change password of postgress user
$ sudo su - postgres
$ psql
=# alter user postgres with password 'postgres';
ALTER ROLE
4. Change configruation for remote access
$ cd /var/lib/pgsql/14/data
$ diff -u postgresql.conf.orig postgresql.conf
--- postgresql.conf.orig 2023-11-05 09:22:02.995785902 +0800
+++ postgresql.conf 2023-11-05 10:00:15.168884636 +0800
@@ -57,7 +57,7 @@
# - Connection Settings -
-#listen_addresses = 'localhost' # what IP address(es) to listen on;
+listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
$ diff -u pg_hba.conf.orig pg_hba.conf
--- pg_hba.conf.orig 2023-11-05 09:22:02.995785902 +0800
+++ pg_hba.conf 2023-11-05 10:00:15.169884642 +0800
@@ -85,6 +85,7 @@
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
+host all all 0.0.0.0/0 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
Then logoff postgres user and restart db service
$ exit
$ sudo systemctl restart postgresql-14
$ psql -hcentos5 -dpostgres -Upostgres # to access db on centos5 remotely
Password for user postgres:
psql (14.9)
Type "help" for help.
postgres=#
5. create (and drop) database if need
postgres=# create database manga;
CREATE DATABAS
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
manga | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
...
postgres=# \c manga
You are now connected to database "manga" as user "postgres".
manga=#
postgres=# drop database manga;
DROP DATABASE
6. create a user "manga" and grant it privileges of default database "postgres"
postgres=# create user manga with password 'manga';
CREATE ROLE
postgres=# \du
postgres=# grant all privileges on database postgres to manga;
postgres=# \q
7. create a schema "manga" and a table "fruit" by using new user "manga"
$ psql -hcentos5 -dpostgres -Umanga
Password for user manga:
psql (14.9)
Type "help" for help.
postgres=> create schema manga;
CREATE SCHEMA
postgres=> \dn
List of schemas
Name | Owner
--------+----------
manga | manga
public | postgres
(2 rows)
postgres=> CREATE TABLE fruit (
fruit_id smallint NOT NULL,
name varchar(10) NOT NULL UNIQUE,
price float NOT NULL,
PRIMARY KEY (fruit_id)
);
CREATE TABLE
postgres=> \d
List of relations
Schema | Name | Type | Owner
--------+-------+-------+-------
manga | fruit | table | manga
(1 row)
postgres=> INSERT INTO fruit VALUES (101,'',800),(102,'',150),(103,'',120),(104,'',200),(105,115),(106,'',110);
INSERT 0 6
postgres=> select * from fruit;
fruit_id | name | price
----------+------+-------
101 | | 800
102 | | 150
103 | | 120
104 | | 200
105 | | 115
106 | | 110
(6 rows)