美文网首页
linux-安装postgresql服务机,并在客户机上验证

linux-安装postgresql服务机,并在客户机上验证

作者: Tess鱼丸 | 来源:发表于2022-11-30 14:23 被阅读0次

[root@dns ~]# ls -al /mnt/cdrom/
总用量 53
dr-xr-xr-x. 7 root root 2048 4月 4 2020 .
drwxr-xr-x. 4 root root 31 10月 31 14:54 ..
dr-xr-xr-x. 4 root root 2048 4月 4 2020 AppStream
dr-xr-xr-x. 4 root root 2048 4月 4 2020 BaseOS
-r--r--r--. 1 root root 60 4月 4 2020 .discinfo
dr-xr-xr-x. 3 root root 2048 4月 4 2020 EFI
-r--r--r--. 1 root root 8266 4月 4 2020 EULA
-r--r--r--. 1 root root 1455 4月 4 2020 extra_files.json
-r--r--r--. 1 root root 18092 4月 4 2020 GPL
dr-xr-xr-x. 3 root root 2048 4月 4 2020 images
dr-xr-xr-x. 2 root root 2048 4月 4 2020 isolinux
-r--r--r--. 1 root root 103 4月 4 2020 media.repo
-r--r--r--. 1 root root 1669 4月 4 2020 RPM-GPG-KEY-redhat-beta
-r--r--r--. 1 root root 5134 4月 4 2020 RPM-GPG-KEY-redhat-release
-r--r--r--. 1 root root 1796 4月 4 2020 TRANS.TBL
-r--r--r--. 1 root root 1560 4月 4 2020 .treeinfo
[root@dns ~]#
[root@dns ~]#

用命令“ rpm -qa | grep postgresql”,发现没有安装

[root@dns ~]# 
[root@dns ~]# rpm -qa |grep postgresql
[root@dns ~]# 
[root@dns ~]# 

用命令” dnf clean all“,清除缓存。
用命令” dnf install @postgresql -y“,安装

[root@dns ~]# dnf install @postgresql -y
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:0:00:56 前,执行于 2022年12月01日 星期四 08时10分25秒。
依赖关系解决。
================================================================================
 软件包             架构    版本                               仓库        大小
================================================================================
安装组/模块包:
 postgresql-server  x86_64  10.6-1.module+el8+2469+5ecd5aae    AppStream  5.1 M
安装依赖关系:
 libpq              x86_64  12.1-3.el8                         AppStream  195 k
 postgresql         x86_64  10.6-1.module+el8+2469+5ecd5aae    AppStream  1.5 M
安装模块配置档案:
 postgresql/server                                                             
启用模块流:
 postgresql                 10                                                 

事务概要
================================================================================
安装  3 软件包

总计:6.7 M
安装大小:27 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                 1/1 
  安装    : libpq-12.1-3.el8.x86_64                                         1/3 
  安装    : postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64               2/3 
  运行脚本: postgresql-server-10.6-1.module+el8+2469+5ecd5aae.x86_64        3/3 
  安装    : postgresql-server-10.6-1.module+el8+2469+5ecd5aae.x86_64        3/3 
  运行脚本: postgresql-server-10.6-1.module+el8+2469+5ecd5aae.x86_64        3/3 
  验证    : libpq-12.1-3.el8.x86_64                                         1/3 
  验证    : postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64               2/3 
  验证    : postgresql-server-10.6-1.module+el8+2469+5ecd5aae.x86_64        3/3 
Installed products updated.

已安装:
  libpq-12.1-3.el8.x86_64                                                       
  postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64                             
  postgresql-server-10.6-1.module+el8+2469+5ecd5aae.x86_64                      

完毕!
[root@dns ~]# 

再次查看是否已安装PostgreSQL
[root@dns ~]# rpm -qa |grep postgresql
postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64
postgresql-server-10.6-1.module+el8+2469+5ecd5aae.x86_64
[root@dns ~]#

需要先初始化PostgreSQL数据库,才能启动。
使用命令:postgresql-setup initdb

[root@dns ~]# postgresql-setup initdb
WARNING: using obsoleted argument syntax, try --help
WARNING: arguments transformed to: postgresql-setup --initdb --unit postgresql
 * Initializing database in '/var/lib/pgsql/data'
 * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
[root@dns ~]# 

启动PostgreSQL数据库

[root@dns ~]# systemctl start postgresql
[root@dns ~]# 
查看PostgreSQL数据库状态
[root@dns ~]# systemctl status postgresql
● postgresql.service - PostgreSQL database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor>
   Active: active (running) since Thu 2022-12-01 08:16:25 EST; 15s ago
  Process: 5493 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (c>
 Main PID: 5496 (postmaster)
    Tasks: 8 (limit: 11160)
   Memory: 20.2M
   CGroup: /system.slice/postgresql.service
           ├─5496 /usr/bin/postmaster -D /var/lib/pgsql/data
           ├─5497 postgres: logger process   
           ├─5500 postgres: checkpointer process   
           ├─5501 postgres: writer process   
           ├─5502 postgres: wal writer process   
           ├─5503 postgres: autovacuum launcher process   
           ├─5504 postgres: stats collector process   
           └─5505 postgres: bgworker: logical replication launcher   

12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa systemd[1]: Starting Pos>
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa postmaster[5496]: 2022-1>
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa postmaster[5496]: 2022-1>
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa postmaster[5496]: 2022-1>
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa postmaster[5496]: 2022-1>
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa postmaster[5496]: 2022-1>
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa postmaster[5496]: 2022-1>
12月 01 08:16:25 dns.long90.cn.10.168.192.in-addr.arpa systemd[1]: Started Post>
[root@dns ~]# 

设置PostgreSQL开机自启动

[root@dns ~]# systemctl enable postgresql
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service → /usr/lib/systemd/system/postgresql.service.
[root@dns ~]# 

登录PostgreSQL。
PostgreSQL默认只能用预定义的数据库超级用户postgres的身份登录,
而不能用root用户登录。

使用命令:su postgres 切换到用户postgres


[root@dns ~]# su - postgres 
[postgres@dns ~]$ 
[postgres@dns ~]$ 
[postgres@dns ~]$ 

使用命令:psql 进入到psql环境。
提示符是“postgres”,意思是当前数据库是postgres

[postgres@dns ~]$ psql
psql (10.6)
输入 "help" 来获取帮助信息.
postgres=# 
postgres=# 
)在psql环境下,可以执行psql元命令。所谓元命令就是由反斜杠开头,后加若干字母组成的命令。
用“\l”元命令,列出服务器上的所有数据库

postgres=# \l
                                     数据库列表
   名称    |  拥有者  | 字元编码 |  校对规则   |    Ctype    |       存取权限   
     
-----------+----------+----------+-------------+-------------+------------------
-----
 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres      
    +
           |          |          |             |             | postgres=CTc/post
gres
 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres      
    +
           |          |          |             |             | postgres=CTc/post
gres
(3 行记录)

postgres=# 

用“\c template1”元命令,切换当前数据库为template1

postgres=# \c template1
您现在已经连接到数据库 "template1",用户 "postgres".
template1=# 

在psql环境下,可以使用SQL语句。
使用“create database db1;”创建数据库db1
注意一定要加分号。

template1=# create database db1;
CREATE DATABASE
template1=# \l
                                     数据库列表
   名称    |  拥有者  | 字元编码 |  校对规则   |    Ctype    |       存取权限        
-----------+----------+----------+-------------+-------------+-----------------------
 db1       | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 行记录)

template1=# 

用“\c db1”元命令,切换当前数据库为db1

template1=# \c db1
您现在已经连接到数据库 "db1",用户 "postgres".
db1=# 

用“create table employee”创建表employee。
三个字段:编号,姓名,性别

db1=# create table employee(
db1(# no char(20),
db1(# name char(30),
db1(# gender char(2)
db1(# );
错误:  关系 "employee" 已经存在
db1=# 

用“select * from employee;”查看表employee的字段

db1=# select * from employee;
 no | name | gender 
----+------+--------
(0 行记录)

db1=# ^C

用insert语句向 employee表添加两条记录

db1=# insert into employee values 
db1-# ('0002','Tom','M');
INSERT 0 1
db1=# insert into employee values
db1-# ('0002','Tom','M');
INSERT 0 1
db1=# ^C

db1=# select * from employee;
          no          |              name              | gender 
----------------------+--------------------------------+--------
 0002                 | Tom                            | M 
 0001                 | Alice                          | F 
(2 行记录)

db1=# 

用drop table语句 删除 employee表

db1=# drop table employee;

“\d”元命令,发现db1数据库中没有表了
由于我并没有执行删除命令所以还是可以看到数据表的

db1=# \d
                关联列表
 架构模式 |   名称   |  类型  |  拥有者  
----------+----------+--------+----------
 public   | employee | 数据表 | postgres
(1 行记录)

db1=# 

“\c postgres”元命令,切换到postgres数据库

db1=# \c postgres 
您现在已经连接到数据库 "postgres",用户 "postgres".
postgres=# 

“ 用drop database语句 删除 数据库db1

postgres=# drop database db1;
DROP DATABASE

“\ q”元命令,退出数据库

postgres=# \q
[postgres@dns ~]$

使用命令:ss -nlt查看服务器端口号5432,正在监听本机

[postgres@dns ~]$ ss -nlt
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     
LISTEN      0           128                    0.0.0.0:111                 0.0.0.0:*        
LISTEN      0           10                192.168.10.1:53                  0.0.0.0:*        
LISTEN      0           10               192.168.122.1:53                  0.0.0.0:*        
LISTEN      0           10                   127.0.0.1:53                  0.0.0.0:*        
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*        
LISTEN      0           5                    127.0.0.1:631                 0.0.0.0:*        
(5432是postgreSQL的端口)              
LISTEN      0           128                  127.0.0.1:5432  (5432是postgreSQL的端口)     0.0.0.0:*        
(5432是postgreSQL的端口)              
LISTEN      0           128                  127.0.0.1:953                 0.0.0.0:*        
LISTEN      0           128                       [::]:111                    [::]:*        
LISTEN      0           128                          *:80                        *:*        
LISTEN      0           10                       [::1]:53                     [::]:*        
LISTEN      0           32                           *:21                        *:*        
LISTEN      0           128                       [::]:22                     [::]:*        
LISTEN      0           5                        [::1]:631                    [::]:*        
LISTEN      0           128                      [::1]:5432                   [::]:*        
LISTEN      0           128                      [::1]:953                    [::]:*        
[postgres@dns ~]$ 

配置客户端远程访问PostgreSQL数据库

postgres用户本来没有密码,现设置postgres用户密码

[postgres@dns ~]$ psql
psql (10.6)
输入 "help" 来获取帮助信息.

postgres=# \password postgres 
输入新的密码:
再次输入:
postgres=# 
#推出psql
postgres=# \q

编辑主配置文件/var/lib/pgsql/data/postgresql.conf,
使PostgreSQL服务器能够在所有可用网络上侦听。默认情况是只有本机
可以侦听。
先对配置文件做备份,再修改。

[postgres@dns ~]$ cd /var/lib/pgsql/data/
[postgres@dns data]$ 
[postgres@dns data]$ ls
base              pg_hba.conf    pg_replslot   pg_tblspc             postgresql.conf
current_logfiles  pg_ident.conf  pg_serial     pg_twophase           postmaster.opts
global            pg_log         pg_snapshots  PG_VERSION            postmaster.pid
log               pg_logical     pg_stat       pg_wal
pg_commit_ts      pg_multixact   pg_stat_tmp   pg_xact
pg_dynshmem       pg_notify      pg_subtrans   postgresql.auto.conf
[postgres@dns data]$ cp postgresql.conf postgresql.conf.bak
[postgres@dns data]$ 

找到这一行:#listen_addresses =‘localhost’
修改为:listen_addresses =‘
把#号去掉,localhost改为
,意思是任意IP都可以侦听
改好后存盘退出

[postgres@dns data]$ vim postgresql.conf
[postgres@dns data]$ 
重启postgresql 
[postgres@dns data]$ systemctl restart postgresql
[postgres@dns data]$ 
[postgres@dns data]$ 
再查看postgersql的
现在就改为0.0.0.0地址监听5432了
[postgres@dns data]$ ss -nlt
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     
LISTEN      0           128                    0.0.0.0:111                 0.0.0.0:*        
LISTEN      0           10                192.168.10.1:53                  0.0.0.0:*        
LISTEN      0           10               192.168.122.1:53                  0.0.0.0:*        
LISTEN      0           10                   127.0.0.1:53                  0.0.0.0:*        
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*        
LISTEN      0           5                    127.0.0.1:631                 0.0.0.0:*        
(5432是postgreSQL的端口)  
LISTEN      0           128                    0.0.0.0:5432                0.0.0.0:*        
(5432是postgreSQL的端口)  
LISTEN      0           128                  127.0.0.1:953                 0.0.0.0:*        
LISTEN      0           128                       [::]:111                    [::]:*        
LISTEN      0           128                          *:80                        *:*        
LISTEN      0           10                       [::1]:53                     [::]:*        
LISTEN      0           32                           *:21                        *:*        
LISTEN      0           128                       [::]:22                     [::]:*        
LISTEN      0           5                        [::1]:631                    [::]:*        
LISTEN      0           128                       [::]:5432                   [::]:*        
LISTEN      0           128                      [::1]:953                    [::]:*        
[postgres@dns data]$ 


启用MD5加密的密码身份验证
先对配置文件pg_hba.conf做备份,再修改。

[postgres@dns data]$ cp pg_hba.conf pg_hba.conf.bak
[postgres@dns data]$ 
[postgres@dns data]$ echo "host all all 0.0.0.0/0 md5">>pg_hba.conf
[postgres@dns data]$ 

===================================================

===================================================

在防火墙上允许数据库服务

[postgres@dns data]$ firewall-cmd --permanent --add-service=postgresql
success
[postgres@dns data]$ 
[postgres@dns data]$ firewall-cmd --reload 
success
[postgres@dns data]$ 

然后到客户机上验证连接

[root@localhost ~]# df -l
文件系统                 1K-块    已用     可用 已用% 挂载点
devtmpfs                892844       0   892844    0% /dev
tmpfs                   921932       0   921932    0% /dev/shm
tmpfs                   921932    9928   912004    2% /run
tmpfs                   921932       0   921932    0% /sys/fs/cgroup
/dev/mapper/rhel-root 17811456 4496616 13314840   26% /
/dev/nvme0n1p1         1038336  234200   804136   23% /boot
tmpfs                   184384    1168   183216    1% /run/user/42
tmpfs                   184384    4644   179740    3% /run/user/0
/dev/sr0               8238560 8238560        0  100% /run/media/root/RHEL-8-2-0-BaseOS-x86_64
[root@localhost ~]# 
[root@localhost ~]# mount /dev/sr0 /mnt/cdrom/
mount: /mnt/cdrom: WARNING: device write-protected, mounted read-only.
[root@localhost ~]#  
[root@localhost ~]# rpm -qa |grep postgresql

[root@localhost ~]# 
[root@localhost ~]# dnf install postgresql -y
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
BaseOS                                          2.7 MB/s | 2.8 kB     00:00    
AppStream                                       3.1 MB/s | 3.2 kB     00:00    
依赖关系解决。
================================================================================
 软件包        架构      版本                                仓库          大小
================================================================================
安装:
 postgresql    x86_64    10.6-1.module+el8+2469+5ecd5aae     AppStream    1.5 M
安装依赖关系:
 libpq         x86_64    12.1-3.el8                          AppStream    195 k
启用模块流:
 postgresql              10                                                    

事务概要
================================================================================
安装  2 软件包

总计:1.7 M
安装大小:6.4 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                 1/1 
  安装    : libpq-12.1-3.el8.x86_64                                         1/2 
  安装    : postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64               2/2 
  运行脚本: postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64               2/2 
  验证    : libpq-12.1-3.el8.x86_64                                         1/2 
  验证    : postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64               2/2 
Installed products updated.

已安装:
  libpq-12.1-3.el8.x86_64   postgresql-10.6-1.module+el8+2469+5ecd5aae.x86_64  

完毕!
[root@localhost ~]# 

在客户机上登录

psql -h 192.168.10.1 -U postgres

[root@localhost ~]# psql -h 192.168.10.1 -U postgres
用户 postgres 的口令:
psql (10.6)
输入 "help" 来获取帮助信息.

postgres=# \l
                                     数据库列表
   名称    |  拥有者  | 字元编码 |  校对规则   |    Ctype    |       存取权限   
     
-----------+----------+----------+-------------+-------------+------------------
-----
 db1       | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 db2       | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres      
    +
           |          |          |             |             | postgres=CTc/post
gres
 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres      
    +
           |          |          |             |             | postgres=CTc/post
gres
(5 行记录)

postgres=# 

进入db1 再查看employee的表

postgres=# \c db
致命错误:  数据库 "db" 不存在
保留上一次连接
postgres=# \c db1
您现在已经连接到数据库 "db1",用户 "postgres".
db1=# 
db1=# select * from employee;
          no          |              name              | gender 
----------------------+--------------------------------+--------
 0002                 | Tom                            | M 
 0001                 | Alice                          | F 
(2 行记录)

db1=# 

相关文章

网友评论

      本文标题:linux-安装postgresql服务机,并在客户机上验证

      本文链接:https://www.haomeiwen.com/subject/dnxsfdtx.html