Fedora 41
Sponsored Link

PostgreSQL 16 : Install2024/11/11

 
Install PostgreSQL to configure database server.
[1] Install and start PostgreSQL.
[root@www ~]#
dnf -y install postgresql-server
[root@www ~]#
postgresql-setup --initdb

 * Initializing database in '/var/lib/pgsql/data'
 * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
 
[root@www ~]#
systemctl enable --now postgresql

[2] By default setting, it's possible to connect to PostgreSQL Server only from Localhost with [peer] authentication.
Refer to the official site below about details of authentication methods.
⇒ https://www.postgresql.jp/document/10/html/auth-pg-hba-conf.html
# listen only localhost by default

[root@www ~]#
grep listen_addresses /var/lib/pgsql/data/postgresql.conf

#listen_addresses = 'localhost' # what IP address(es) to listen on;
# authentication methods by default

[root@www ~]#
grep -v -E "^#|^$" /var/lib/pgsql/data/pg_hba.conf

local   all             all                                     peer
host    all             all             127.0.0.1/32            ident
host    all             all             ::1/128                 ident
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident
[3] On [peer] authentication, it needs OS user and PostgreSQL user whose name are the same to connect to PostgreSQL Server.
# add an OS user

[root@www ~]#
useradd fedora
# add an PostgreSQL user and his Database with PostgreSQL admin user

[root@www ~]#
su - postgres

[postgres@www ~]$
createuser fedora

[postgres@www ~]$
createdb testdb -O fedora
# show users and databases

[postgres@www ~]$
psql -c "select usename from pg_user;"
 usename
----------
 postgres
 fedora
(2 rows)

[postgres@www ~]$
psql -l
                                                 List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
 testdb    | fedora   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
(4 rows)
[4] Try to connect to PostgreSQL Database with a user added above.
# connect to testdb

[fedora@www ~]$
psql testdb

psql (16.3)
Type "help" for help.

# show user roles
testdb=> \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 fedora    |                                                            | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

# show databases
testdb=> \l
                                                 List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
 testdb    | fedora   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
(4 rows)

# create a test table
testdb=> create table test_table (no int, name text); 
CREATE TABLE

# show tables
testdb=> \dt 
          List of relations
 Schema |    Name    | Type  | Owner
--------+------------+-------+--------
 public | test_table | table | fedora
(1 row)

# insert data to test table
testdb=> insert into test_table (no,name) values (01,'Fedora'); 
INSERT 0 1

# confirm
testdb=> select * from test_table; 
 no |  name
----+--------
  1 | Fedora
(1 row)

# remove test table
testdb=> drop table test_table; 
DROP TABLE

testdb=> \dt 
Did not find any relations.

# exit
testdb=> \q 

# remove testdb

[fedora@www ~]$
dropdb testdb

[fedora@www ~]$
psql -l

                                                 List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
(3 rows)
Matched Content