暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

简明postgresql安装和配置

追梦IT人 2022-12-17
494

因前端代码连接pg的需要,又参考之前的文档重新安装了一遍postgresql,想一次性过还是有点小问题,涉及防火墙、配置文件的修改等等。

1、直接通过yum安装postgresql-server和postgresql-contrib。

[root@localhost lib]# yum install postgresql-server
Loaded plugins: fastestmirror
Determining fastest mirrors
Could not get metalink https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=x86_64 error was
12: Timeout on https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=x86_64: (28'Operation timed out after 30001 milliseconds with 0 out of 0 bytes received')
 * base: mirrors.cqu.edu.cn
 * epel: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.cqu.edu.cn
 * updates: mirrors.cqu.edu.cn
base                                                                                               | 3.6 kB  00:00:00     
extras                                                                                             | 2.9 kB  00:00:00     
mysql-connectors-community                                                                         | 2.6 kB  00:00:00     
mysql-tools-community                                                                              | 2.6 kB  00:00:00     
mysql80-community                                                                                  | 2.6 kB  00:00:00     
https://packagecloud.io/rabbitmq/erlang/el/7/x86_64/repodata/repomd.xml: [Errno 12] Timeout on https://d28dx6y1hfq314.cloudfront.net/828/1039/el/7/x86_64/repodata/f529b4a78e15bfff91a18ffa428f84e44173906fd12ad40ad2c914dfbba4ad7b-repomd.xml?t=1671159862_40278d5a1b11edb691c5e24a1e2778007e7413cc: (28'Resolving timed out after 30535 milliseconds')
Trying other mirror.
rabbitmq_erlang/x86_64/signature                                                                   |  833 B  00:00:00     
rabbitmq_erlang/x86_64/signature                                                                   | 1.8 kB  00:00:00 !!! 
rabbitmq_erlang-source/signature                                                                   |  819 B  00:00:00     
rabbitmq_erlang-source/signature                                                                   |  951 B  00:00:00 !!! 
rabbitmq_rabbitmq-server/x86_64/signature                                                          |  833 B  00:00:00     
rabbitmq_rabbitmq-server/x86_64/signature                                                          | 1.8 kB  00:00:00 !!! 
rabbitmq_rabbitmq-server-source/signature                                                          |  836 B  00:00:00     
rabbitmq_rabbitmq-server-source/signature                                                          | 1.0 kB  00:00:00 !!! 
updates                                                                                            | 2.9 kB  00:00:00     
wandisco-git                                                                                       | 2.9 kB  00:00:00     
updates/7/x86_64/primary_db                                                                        |  18 MB  00:00:03     
Resolving Dependencies
--> Running transaction check
---> Package postgresql-server.x86_64 0:9.2.24-8.el7_9 will be installed
--> Processing Dependency: postgresql-libs(x86-64) = 9.2.24-8.el7_9 for package: postgresql-server-9.2.24-8.el7_9.x86_64
--> Processing Dependency: postgresql(x86-64) = 9.2.24-8.el7_9 for package: postgresql-server-9.2.24-8.el7_9.x86_64
--> Processing Dependency: libpq.so.5()(64bit) for package: postgresql-server-9.2.24-8.el7_9.x86_64
--> Running transaction check
---> Package postgresql.x86_64 0:9.2.24-8.el7_9 will be installed
---> Package postgresql-libs.x86_64 0:9.2.24-8.el7_9 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================
 Package                           Arch                   Version                           Repository               Size
==========================================================================================================================
Installing:
 postgresql-server                 x86_64                 9.2.24-8.el7_9                    updates                 3.8 M
Installing for dependencies:
 postgresql                        x86_64                 9.2.24-8.el7_9                    updates                 3.0 M
 postgresql-libs                   x86_64                 9.2.24-8.el7_9                    updates                 235 k

Transaction Summary
==========================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 7.1 M
Installed size: 33 M
Is this ok [y/d/N]: y
Downloading packages:
(1/
3): postgresql-libs-9.2.24-8.el7_9.x86_64.rpm                                                   | 235 kB  00:00:00     
(2/3): postgresql-9.2.24-8.el7_9.x86_64.rpm                                                        | 3.0 MB  00:00:00     
(3/3): postgresql-server-9.2.24-8.el7_9.x86_64.rpm                                                 | 3.8 MB  00:00:02     
--------------------------------------------------------------------------------------------------------------------------
Total                                                                                     2.7 MB/s | 7.1 MB  00:00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : postgresql-libs-9.2.24-8.el7_9.x86_64                                                                  1/3 
  Installing : postgresql-9.2.24-8.el7_9.x86_64                                                                       2/3 
  Installing : postgresql-server-9.2.24-8.el7_9.x86_64                                                                3/3 
  Verifying  : postgresql-server-9.2.24-8.el7_9.x86_64                                                                1/3 
  Verifying  : postgresql-libs-9.2.24-8.el7_9.x86_64                                                                  2/3 
  Verifying  : postgresql-9.2.24-8.el7_9.x86_64                                                                       3/3 

Installed:
  postgresql-server.x86_64 0:9.2.24-8.el7_9                                                                               

Dependency Installed:
  postgresql.x86_64 0:9.2.24-8.el7_9                        postgresql-libs.x86_64 0:9.2.24-8.el7_9                       

Complete!
[root@localhost lib]# yum install postgresql-contrib
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                               |  10 kB  00:00:00     
 * base: mirrors.cqu.edu.cn
 * epel: mirror.01link.hk
 * extras: mirrors.cqu.edu.cn
 * updates: mirrors.cqu.edu.cn
epel                                                                                               | 4.7 kB  00:00:00     
(1/3): epel/x86_64/group_gz                                                                        |  98 kB  00:00:00     
(2/3): epel/x86_64/updateinfo                                                                      | 1.0 MB  00:00:00     
(3/3): epel/x86_64/primary_db                                                                      | 7.0 MB  00:00:01     
Resolving Dependencies
--> Running transaction check
---> Package postgresql-contrib.x86_64 0:9.2.24-8.el7_9 will be installed
--> Processing Dependency: libossp-uuid.so.16()(64bit) for package: postgresql-contrib-9.2.24-8.el7_9.x86_64
--> Running transaction check
---> Package uuid.x86_64 0:1.6.2-26.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================================================
 Package                            Arch                   Version                          Repository               Size
==========================================================================================================================
Installing:
 postgresql-contrib                 x86_64                 9.2.24-8.el7_9                   updates                 553 k
Installing for dependencies:
 uuid                               x86_64                 1.6.2-26.el7                     base                     55 k

Transaction Summary
==========================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 608 k
Installed size: 1.9 M
Is this ok [y/d/N]: y
Downloading packages:
(1/
2): uuid-1.6.2-26.el7.x86_64.rpm                                                                |  55 kB  00:00:00     
(2/2): postgresql-contrib-9.2.24-8.el7_9.x86_64.rpm                                                | 553 kB  00:00:00     
--------------------------------------------------------------------------------------------------------------------------
Total                                                                                     1.4 MB/s | 608 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : uuid-1.6.2-26.el7.x86_64                                                                               1/2 
  Installing : postgresql-contrib-9.2.24-8.el7_9.x86_64                                                               2/2 
  Verifying  : uuid-1.6.2-26.el7.x86_64                                                                               1/2 
  Verifying  : postgresql-contrib-9.2.24-8.el7_9.x86_64                                                               2/2 

Installed:
  postgresql-contrib.x86_64 0:9.2.24-8.el7_9                                                                              

Dependency Installed:
  uuid.x86_64 0:1.6.2-26.el7                                                                                              

Complete!

[root@localhost lib]# rpm -aq|grep postgres
postgresql-libs-9.2.24-8.el7_9.x86_64
postgresql-contrib-9.2.24-8.el7_9.x86_64
postgresql-9.2.24-8.el7_9.x86_64
postgresql-server-9.2.24-8.el7_9.x86_64


复制

2、初始化数据库,并启动和查看postgresql

[root@localhost lib]# postgresql-setup initdb
Initializing database ... OK

[root@localhost lib]# systemctl start postgresql
[root@localhost lib]# systemctl status postgresql
● postgresql.service - PostgreSQL database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2022-12-16 11:04:56 CST; 11s ago
  Process: 28364 ExecStart=/usr
/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=0/SUCCESS)
  Process: 28358 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 28367 (postgres)
   CGroup: /system.slice/postgresql.service
           ├─28367 /usr/bin/postgres -D /var/lib/pgsql/data -p 5432
           ├─28368 postgres: logger process   
           ├─28370 postgres: checkpointer process   
           ├─28371 postgres: writer process   
           ├─28372 postgres: wal writer process   
           ├─28373 postgres: autovacuum launcher process   
           └─28374 postgres: stats collector process   

Dec 16 11:04:55 localhost.localdomain systemd[1]: Starting PostgreSQL database server...
Dec 16 11:04:56 localhost.localdomain systemd[1]: Started PostgreSQL database server.
[root@localhost lib]# systemctl enable postgresql
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql.service to /usr/lib/systemd/system/postgresql.service.


复制

3、开启访问端口和启用防火墙

小插曲提示firewalld is not running,需要启动防火墙

[root@localhost lib]# firewall-cmd --permanent --add-port=5432/tcp
FirewallD is not running
[root@localhost lib]# systemctl start firewalld.service
[root@localhost lib]# systemctl enable firewalld.service
[root@localhost lib]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2022-12-16 11:08:34 CST; 16s ago
     Docs: man:firewalld(1)
 Main PID: 28993 (firewalld)
   CGroup: /system
.slice/firewalld.service
           └─28993 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Dec 16 11:08:33 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Dec 16 11:08:34 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
[root@localhost lib]# firewall-cmd --permanent --add-port=5432/tcp
success
[root@localhost lib]# firewall-cmd --reload
success


复制

4、进入postgresql进行测试

先通过psql进入pg,修改口令

[root@localhost lib]# su - postgres
-bash-4.2$ psql -U postgres
psql (9.2.24)
Type "help" for help.
postgres=# ALTER USER postgres WITH PASSWORD '*******';
ALTER ROLE
postgres=# ^Z
[1]+  Stopped                 psql -U postgres
-bash-4.2


复制

5、通过Navicat客户端配置pg连接,无法访问数据库

6、进入数据库,查看监听地址,发线默认是localhost监听

-bash-4.2$ psql -U postgres      
psql (9.2.24)
Type "help" for help.

postgres=# SHOW listen_addresses;
 listen_addresses 
------------------
 localhost
(1 row)


复制

7、修改postgresql.conf和pg_hba.conf文件,再重启

-bash-4.2$ cd /var/lib/pgsql/data

-bash-4.2$ vi postgresql.conf 
# -----------------------------
# PostgreSQL configuration file
# -----------------------------
#
listen_addresses = '*'          # what IP address(es) to listen on;


-bash-4.2$ vi pg_hba.conf 
# PostgreSQL Client Authentication Configuration File
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
host    all             all             0.0.0.0/0             md5
"pg_hba.conf" 90L, 4302C written
"postgresql.conf" 578L, 19836C written

[root@localhost ~]# systemctl restart postgresql


复制

8、再次通过Navicat客户端配置pg连接,访问数据库就OK了


文章转载自追梦IT人,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论