「広告」

PosgreSQL、初めてのPostgreSQLインストール

「広告」
記事内に広告が含まれています。
「広告」

PostgreSQLの初期設定の覚え書きです。

PostgreSQLは、2020年5月の時点で、
Version 12.3系、11.8系、10.13系、9.6系、9.5系がリリースされていているようです。
9.4系は最近メンテナンスが終了をしているようです。

出展:https://www.postgresql.org/support/versioning/

VersionマイナーVersion初期リリース最終リリース
1212.32019/10/32024/11/14
1111.82018/10/182023/11/9
1010.132017/10/52022/11/10
9.69.6.182016/9/292021/11/11
9.59.5.222016/1/72021/2/11

(横にスクロールできます)

今回は、yumで、インストールする、「version 10系」を入れます。
version 10 系は、2022年11月10の最終リリースまで、サポートされるようです。
あと、2年少しは、大丈夫です(笑)

今回は、「ConoHaVPS」で入れました。

「広告」

OS初期設定

# cat /etc/centos-release
CentOS Linux release 7.8.2003 (Core)

↑CentOS 7.8です

# yum update
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp-srv2.kddilabs.jp
 * epel: nrt.edge.kernel.org
 * epel-debuginfo: nrt.edge.kernel.org
 * epel-source: nrt.edge.kernel.org
 * extras: ftp-srv2.kddilabs.jp
 * updates: ftp-srv2.kddilabs.jp
Resolving Dependencies
--> Running transaction check

(略)

  Cleanup    : 32:bind-export-libs-9.11.4-16.P2.el7_8.3.x86_64                                                                                    18/19 
  Cleanup    : python-perf-3.10.0-1127.8.2.el7.x86_64                                                                                             19/19 
yum install openssl-devel
  Verifying  : kernel-3.10.0-1127.10.1.el7.x86_64                                                                                                  1/19 
  Verifying  : kernel-tools-3.10.0-1127.10.1.el7.x86_64                                                                                            2/19 

↑update完了です

「広告」

PostgreSQLインストール

# yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

↑リポジトリを追加します。

# yum info postgresql10-server

(略)

Name        : postgresql10-server
Arch        : x86_64
Version     : 10.13
Release     : 1PGDG.rhel7
Size        : 4.5 M
Repo        : pgdg10/7/x86_64
Summary     : The programs needed to create and run a PostgreSQL server
URL         : https://www.postgresql.org/
License     : PostgreSQL
Description : PostgreSQL is an advanced Object-Relational database management system (DBMS).
            : The postgresql10-server package contains the programs needed to create
            : and run a PostgreSQL server, which will in turn allow you to create
            : and maintain PostgreSQL databases.

↑PostgreSQL 10.13がインストールできます。

# yum install postgresql10-server

(略)


Downloading packages:
(1/4): libicu-50.2-4.el7_7.x86_64.rpm                                                                                            | 6.9 MB  00:00:00     
warning: /var/cache/yum/x86_64/7/pgdg10/packages/postgresql10-libs-10.13-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEYTA 
Public key for postgresql10-libs-10.13-1PGDG.rhel7.x86_64.rpm is not installed
(2/4): postgresql10-libs-10.13-1PGDG.rhel7.x86_64.rpm                                                                            | 357 kB  00:00:02     
(3/4): postgresql10-10.13-1PGDG.rhel7.x86_64.rpm                                                                                 | 1.6 MB  00:00:03     
(4/4): postgresql10-server-10.13-1PGDG.rhel7.x86_64.rpm                                                                          | 4.5 MB  00:00:02     
--------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                   2.8 MB/s |  13 MB  00:00:04     


Installed:
  postgresql10-server.x86_64 0:10.13-1PGDG.rhel7                                                                                                        

Dependency Installed:
  libicu.x86_64 0:50.2-4.el7_7             postgresql10.x86_64 0:10.13-1PGDG.rhel7             postgresql10-libs.x86_64 0:10.13-1PGDG.rhel7            

Complete!

↑PostgreSQL10が入りました。

# /usr/pgsql-10/bin/postgresql-10-setup initdb
Initializing database ... OK

↑PostgreSQL10 の初期設定をします。

# systemctl enable postgresql-10
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-10.service to /usr/lib/systemd/system/postgresql-10.service.
#  systemctl start postgresql-10

↑PostgreSQLの自動起動とサービス開始を行います。

「広告」

PostgreSQLにて、データベース作成

# su - postgres
-bash-4.2$ psql -U postgres
psql (10.13)
Type "help" for help.

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

↑「postgres」ユーザになり、データベースの一覧を見てみます。

DB名:db01
DBuser:user01
DBpass:(DB用パスワード)

↑上記のDBを作成を作成しています。

# su - postgres

postgres=# CREATE ROLE user01 WITH LOGIN PASSWORD '(DB用パスワード)';
CREATE ROLE

↑ユーザを作成して、パスワードも同時に作成します。

postgres=# create database db01 owner user01;
CREATE DATABASE
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 db01      | user01   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

↑作成したユーザ用にデータベースを作成します。

PostgreSQLの場合、ログインユーザと同じDBを作成しないといけません・・・

# su - postgres
-bash-4.2$ psql -U postgres
psql (10.13)
Type "help" for help.

postgres=# create database user01;
CREATE DATABASE
postgres=# \l (←「¥」と「エルの小文字」です)
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 db01      | user01   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 user01    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(5 rows)

↑ユーザ名と同じデータベースが作成されました。

# su - postgres
$ psql -c "alter role postgres with password '(postgres用パスワード)'" ;
ALTER ROLE

「postgres」ユーザのパスワードも変更をしておきます。

「広告」

PostgreSQLの認証方式を変更

# su - postgres
-bash-4.2$ psql -U user01
psql: FATAL:  Peer authentication failed for user "user01"

↑作成したユーザでログインができません。

# vi /var/lib/pgsql/10/data/pg_hba.conf

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident

↑「pg_hba.conf」の下の方の設定を変更します。

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident

↑3カ所「md5」にします。

#  systemctl restart postgresql-10
「広告」

PostgreSQL 設定完了

# su - postgres
-bash-4.2$ psql -U user01
Password for user user01: (DB用パスワード)
psql (10.13)
Type "help" for help.

user01=> \l (←「¥」と「エルの小文字」です)
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 db01      | user01   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 user01    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(5 rows)

↑user01でログインができました。

これで、初期の登録は完了です。



タイトルとURLをコピーしました