从零开始学PostgreSQL (二) & Linux Yum安装PostgreSQL


前言

PostgreSQL以yum在线安装的形式提供给当今大多数常见的操作系统。前提是该系统服务器可以与互联网进行联通。如果处于公司内网等无互联网环境情况下,可以使用rpm包、二进制安装以及源码编译安装三种方式来安装PG数据库。
本文章使用的是Yum方式来进行安装。

环境准备

系统版本:Centos 7.6
数据库版本:PostgreSQL 16.1

安装步骤

一: PostgreSQL官网下载

官网地址:https://www.postgresql.org/download/
image.png

1、选择你需要下载的环境
  • 第一个选择的是系统,选择您的操作系统系列:这里我们系统以Linux中的Centos为例
  • 第二个是选择你环境的发行版本:这里我们选择的是Red Hat/Rocky/AlmaLinux

image.png
选择后会跳转到详细页面,在PostgreSQL Yum Repository模块下继续选择详细信息。
image.png

2、选择你的系统环境
    1. 选择需要安装的Postgresql版本,选择16,默认就会安装13下面最新的版本。
    1. 选择你系统版本号
    1. 选择系统的架构(一般都是默认的,且Centos系统都是x86架构)
    1. 就是查找出来,对应你选择的系统版本安装Postgresql需要执行的命令

image.png

下面是查询出来安装postgresql的命令,将下面的命令一一执行即可完成postgresql的安装。
下面带有# 号的为注释,不需要在系统中执行。

二:安装 postgresql-15

# Install the repository RPM:这里的意思是“安装存储库RPM”
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Install PostgreSQL:这里的意思是“安装PostgreSQL”
sudo yum install -y postgresql15-server
 
# Optionally initialize the database and enable automatic start:这里的意思是 “初始化数据库并设置为开机自动启动”
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
sudo systemctl enable postgresql-15
sudo systemctl start postgresql-15
1、执行安装存储库RPM
[root@VM-16-10-centos ~]# sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Loaded plugins: fastestmirror, langpacks
pgdg-redhat-repo-latest.noarch.rpm                                                       |  10 kB  00:00:00     
Examining /var/tmp/yum-root-K8ykzr/pgdg-redhat-repo-latest.noarch.rpm: pgdg-redhat-repo-42.0-38PGDG.noarch
Marking /var/tmp/yum-root-K8ykzr/pgdg-redhat-repo-latest.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package pgdg-redhat-repo.noarch 0:42.0-38PGDG will be installed
--> Finished Dependency Resolution
epel/7/x86_64                                                                            | 4.7 kB  00:00:00     
extras/7/x86_64                                                                          | 2.9 kB  00:00:00     
os/7/x86_64                                                                              | 3.6 kB  00:00:00     
updates/7/x86_64                                                                         | 2.9 kB  00:00:00     

Dependencies Resolved

================================================================================================================
 Package                   Arch            Version               Repository                                Size
================================================================================================================
Installing:
 pgdg-redhat-repo          noarch          42.0-38PGDG           /pgdg-redhat-repo-latest.noarch           12 k

Transaction Summary
================================================================================================================
Install  1 Package

Total size: 12 k
Installed size: 12 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : pgdg-redhat-repo-42.0-38PGDG.noarch                                                          1/1 
  Verifying  : pgdg-redhat-repo-42.0-38PGDG.noarch                                                          1/1 

Installed:
  pgdg-redhat-repo.noarch 0:42.0-38PGDG      

Complete!    # 出现这里的时候证明该命令已成功执行完成
2、执行安装命令
[root@VM-16-10-centos ~]# sudo yum install -y postgresql15-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package postgresql15-server.x86_64 0:15.5-1PGDG.rhel7 will be installed
--> Processing Dependency: postgresql15-libs(x86-64) = 15.5-1PGDG.rhel7 for package: postgresql15-server-15.5-1PGDG.rhel7.x86_64
--> Processing Dependency: postgresql15(x86-64) = 15.5-1PGDG.rhel7 for package: postgresql15-server-15.5-1PGDG.rhel7.x86_64
--> Processing Dependency: libzstd.so.1()(64bit) for package: postgresql15-server-15.5-1PGDG.rhel7.x86_64
--> Processing Dependency: libpq.so.5()(64bit) for package: postgresql15-server-15.5-1PGDG.rhel7.x86_64
--> Running transaction check
---> Package libzstd.x86_64 0:1.5.5-1.el7 will be installed
---> Package postgresql15.x86_64 0:15.5-1PGDG.rhel7 will be installed
---> Package postgresql15-libs.x86_64 0:15.5-1PGDG.rhel7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================
 Package                          Arch                Version                         Repository           Size
================================================================================================================
Installing:
 postgresql15-server              x86_64              15.5-1PGDG.rhel7                pgdg15              5.8 M
Installing for dependencies:
 libzstd                          x86_64              1.5.5-1.el7                     epel                292 k
 postgresql15                     x86_64              15.5-1PGDG.rhel7                pgdg15              1.6 M
 postgresql15-libs                x86_64              15.5-1PGDG.rhel7                pgdg15              285 k

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

Total download size: 8.0 M
Installed size: 34 M
Downloading packages:
(1/4): libzstd-1.5.5-1.el7.x86_64.rpm                                                    | 292 kB  00:00:00     
warning: /var/cache/yum/x86_64/7/pgdg15/packages/postgresql15-15.5-1PGDG.rhel7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 73e3b907: NOKEY
Public key for postgresql15-15.5-1PGDG.rhel7.x86_64.rpm is not installed
(2/4): postgresql15-15.5-1PGDG.rhel7.x86_64.rpm                                          | 1.6 MB  00:00:32     
(3/4): postgresql15-libs-15.5-1PGDG.rhel7.x86_64.rpm                                     | 285 kB  00:00:37     
(4/4): postgresql15-server-15.5-1PGDG.rhel7.x86_64.rpm                                   | 5.8 MB  00:02:15     
----------------------------------------------------------------------------------------------------------------
Total                                                                            49 kB/s | 8.0 MB  00:02:48     
Retrieving key from file:///etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL7
Importing GPG key 0x73E3B907:
 Userid     : "PostgreSQL RPM Repository <pgsql-pkg-yum@lists.postgresql.org>"
 Fingerprint: f245 f0bf 96ac 1827 44ca ff2e 64fa ce11 73e3 b907
 Package    : pgdg-redhat-repo-42.0-38PGDG.noarch (@/pgdg-redhat-repo-latest.noarch)
 From       : /etc/pki/rpm-gpg/PGDG-RPM-GPG-KEY-RHEL7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : postgresql15-libs-15.5-1PGDG.rhel7.x86_64                                                    1/4 
  Installing : libzstd-1.5.5-1.el7.x86_64                                                                   2/4 
  Installing : postgresql15-15.5-1PGDG.rhel7.x86_64                                                         3/4 
  Installing : postgresql15-server-15.5-1PGDG.rhel7.x86_64                                                  4/4 
  Verifying  : postgresql15-15.5-1PGDG.rhel7.x86_64                                                         1/4 
  Verifying  : libzstd-1.5.5-1.el7.x86_64                                                                   2/4 
  Verifying  : postgresql15-server-15.5-1PGDG.rhel7.x86_64                                                  3/4 
  Verifying  : postgresql15-libs-15.5-1PGDG.rhel7.x86_64                                                    4/4 

Installed:
  postgresql15-server.x86_64 0:15.5-1PGDG.rhel7                                                                 

Dependency Installed:
  libzstd.x86_64 0:1.5.5-1.el7                              postgresql15.x86_64 0:15.5-1PGDG.rhel7              
  postgresql15-libs.x86_64 0:15.5-1PGDG.rhel7              

Complete!  # 出现这里的时候证明数据库已完成安装
3、初始化数据库
[root@VM-16-10-centos ~]# sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
Initializing database ... OK    # 数据库初始化成功
4、配置系统管理
# 设置开机自启动
[root@VM-16-10-centos ~]# sudo systemctl enable postgresql-15
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-15.service to /usr/lib/systemd/system/postgresql-15.service.
# 启动数据库服务
[root@VM-16-10-centos ~]# sudo systemctl start postgresql-15
5、测试数据库

postgresql 在安装时默认添加用户postgres

[root@VM-16-10-centos ~]# su - postgres    # 切换到postgres用户
-bash-4.2$ psql             
psql (15.5)
Type "help" for help.

postgres=# 

三:配置postgresql数据库

1、设置数据库密码
# 修改数据库密码语法   ALTER USER postgres WITH PASSWORD '所需要设置的密码';
postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
ALTER ROLE
postgres=# \q
-bash-4.2$ exit
logout
[root@VM-16-10-centos ~]# 

配置文件说明:

  • postgresql.conf

该文件包含一些通用设置,比如内存分配、新建database的默认存储位置、Postgresql服务器的IP地址、日志位置以及许多其他位置。

  • pg_hba.conf

该文件用于控制Postgresql服务器的访问权限,具体包括:允许哪些用户连接到哪个数据库,允许哪些IP地址连接到本服务器,以及制定连接时使用的身份验证模式。

  • pg_ident.conf

如果该文件存在,则系统会基于文件内容将当前登陆的操作系统用户映射为一个Postgresql数据库内部的身份来登陆。有些人会把操作系统的root用户映射为Postgresql的postgres超级用户账号。

2、修改pg远程连接配置文件

pg_hba.conf

[root@VM-16-10-centos ~]# vim /var/lib/pgsql/15/data/pg_hba.conf
# 快捷键 shift + G   跳转到末尾
# 在IPv4 添加下面内容

# IPv4 local connections:
host    all             all             0.0.0.0/0               md5

image.png

3、修改主配置文件

配置远程连接的地址和端口号

[root@VM-16-10-centos ~]# vim /var/lib/pgsql/15/data/postgresql.conf

放开listen_addresses和port的注释并把listen_addresses后的localhost改成 *
wq保存退出
image.png

4、查看postgresql是否已经在运行
[root@VM-16-10-centos ~]# ps -ef | grep postgresql
root     28661 27487  0 22:24 pts/2    00:00:00 grep --color=auto postgresql
5、服务器放行端口
# 添加指定需要开放的端口:
[root@VM-16-10-centos ~]# firewall-cmd --add-port=5432/tcp --permanent
# 重新载入添加的端口:
[root@VM-16-10-centos ~]# firewall-cmd --reload
# 查询指定端口是否开启成功:
[root@VM-16-10-centos ~]# firewall-cmd --query-port=5432/tcp
6、重启数据库服务
[root@VM-16-10-centos ~]# systemctl restart postgresql-15

至此,数据库已完成安装,可正常使用。

  • 21
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是在Linux系统下通过进制文件安装PostgreSQL的步骤: 1. 首先,从PostgreSQL官网下载适用于你的Linux版本的进制安装文件,例如:postgresql-12.5-linux-x64-binaries.tar.gz。 2. 将下载的文件解压到你想要安装PostgreSQL的目录下,例如:/usr/local/pgsql。使用以下命令: ``` sudo tar -zxvf postgresql-12.5-linux-x64-binaries.tar.gz -C /usr/local/ ``` 3. 在可执行文件中添加PostgreSQL进制目录的路径。使用以下命令: ``` sudo su - echo 'export PATH=$PATH:/usr/local/pgsql/bin' >> /etc/profile.d/postgresql.sh exit ``` 4. 重新启动终端或使用以下命令更新环境变量: ``` source /etc/profile.d/postgresql.sh ``` 5. 创建一个新用户和组来运行PostgreSQL服务。使用以下命令: ``` sudo groupadd postgres sudo useradd -r -g postgres -s /bin/false postgres ``` 6. 创建一个新的数据目录并设置其所有者为postgres用户和组。使用以下命令: ``` sudo mkdir /usr/local/pgsql/data sudo chown -R postgres:postgres /usr/local/pgsql/data ``` 7. 初始化数据库并启动PostgreSQL服务。使用以下命令: ``` sudo -u postgres /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start ``` 8. 设置PostgreSQL的超级用户密码。使用以下命令: ``` sudo -u postgres /usr/local/pgsql/bin/psql -U postgres template1 ``` 进入PostgreSQL终端后,输入以下命令来设置超级用户密码: ``` \password postgres ``` 然后输入新密码并确认。 现在,你已经成功地在Linux系统上安装PostgreSQL,并启动了服务。你可以使用psql等命令行工具或pgAdmin等GUI工具来管理和操作数据库。如果你有任何问题或需要更多帮助,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值