Fedora 36にPostgreSQL 14をインストールする方法

環境
OSバージョンを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# cat /etc/redhat-release
Fedora release 36 (Thirty Six)
# cat /etc/redhat-release Fedora release 36 (Thirty Six)
# cat /etc/redhat-release
Fedora release 36 (Thirty Six)

PostgreSQLバージョンを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# psql -V
psql (PostgreSQL) 14.2
# psql -V psql (PostgreSQL) 14.2
# psql -V
psql (PostgreSQL) 14.2

PostgreSQL 14 のインストール手順
1.PostgreSQL をインストールします

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# dnf module -y install postgresql:14/server
# dnf module -y install postgresql:14/server
# dnf module -y install postgresql:14/server

2.PostgreSQLの初期化を行います

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# postgresql-setup --initdb
* Initializing database in '/var/lib/pgsql/data'
* Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
# postgresql-setup --initdb * Initializing database in '/var/lib/pgsql/data' * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
# postgresql-setup --initdb
 * Initializing database in '/var/lib/pgsql/data'
 * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log

3.PostgreSQLのステータスを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# systemctl status postgresql
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendo>
Active: active (running) since Sat 2022-05-28 20:37:34 JST; 1min 12s ago
# systemctl status postgresql ● postgresql.service - PostgreSQL database server Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendo> Active: active (running) since Sat 2022-05-28 20:37:34 JST; 1min 12s ago
# systemctl status postgresql
● postgresql.service - PostgreSQL database server
     Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendo>
     Active: active (running) since Sat 2022-05-28 20:37:34 JST; 1min 12s ago

4.PostgreSQLのデフォルト設定を確認します
ローカルホスト接続を確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# grep listen_addresses /var/lib/pgsql/data/postgresql.conf
#listen_addresses = 'localhost' # what IP address(es) to listen on;
# grep listen_addresses /var/lib/pgsql/data/postgresql.conf #listen_addresses = 'localhost' # what IP address(es) to listen on;
# grep listen_addresses /var/lib/pgsql/data/postgresql.conf
#listen_addresses = 'localhost'         # what IP address(es) to listen on;

認証方式を確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 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
# 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
# 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

5. PostgreSQL ユーザーを追加します
OSユーザーを追加します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# useradd fedora
# useradd fedora
# useradd fedora

PostgreSQL管理ユーザーで PostgreSQL ユーザーとデータベースを追加します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[root@fedora ~]# su - postgres
[postgres@fedora ~]$ createuser fedora
[postgres@fedora ~]$ createdb cftdb -O fedora
[root@fedora ~]# su - postgres [postgres@fedora ~]$ createuser fedora [postgres@fedora ~]$ createdb cftdb -O fedora
[root@fedora ~]# su - postgres
[postgres@fedora ~]$ createuser fedora
[postgres@fedora ~]$ createdb cftdb -O fedora

PostgreSQLユーザーを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[postgres@fedora ~]$ psql -c "select usename from pg_user;"
usename
----------
postgres
fedora
(2)
[postgres@fedora ~]$ psql -c "select usename from pg_user;" usename ---------- postgres fedora (2 行)
[postgres@fedora ~]$ psql -c "select usename from pg_user;"
 usename
----------
 postgres
 fedora
(2 行)

6.新規追加したユーザーで PostgreSQL を利用します
テストDB に接続します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[postgres@fedora ~]$ psql cftdb
psql (14.2)
"help"でヘルプを表示します。
cftdb=#
[postgres@fedora ~]$ psql cftdb psql (14.2) "help"でヘルプを表示します。 cftdb=#
[postgres@fedora ~]$ psql cftdb
psql (14.2)
"help"でヘルプを表示します。

cftdb=#

ユーザーロール一覧を表示します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cftdb-# \du
ロール一覧
ロール名 | 属性 | 所属グループ
----------+------------------------------------------------------------------------------+--------------
fedora | | {}
postgres | スーパーユーザー, ロール作成可, DB作成可, レプリケーション可, RLS のバイパス | {}
cftdb-# \du ロール一覧 ロール名 | 属性 | 所属グループ ----------+------------------------------------------------------------------------------+-------------- fedora | | {} postgres | スーパーユーザー, ロール作成可, DB作成可, レプリケーション可, RLS のバイパス | {}
cftdb-#  \du
                                               ロール一覧
 ロール名 |                                     属性                                     | 所属グループ
----------+------------------------------------------------------------------------------+--------------
 fedora   |                                                                              | {}
 postgres | スーパーユーザー, ロール作成可, DB作成可, レプリケーション可, RLS のバイパス | {}

テストテーブルを作成します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cftdb=# create table user_tbl (uno int, uname text);
CREATE TABLE
cftdb=# create table user_tbl (uno int, uname text); CREATE TABLE
cftdb=# create table user_tbl (uno int, uname text);
CREATE TABLE

 

Fedora 36

Posted by arkgame