一:PostgreSQ官网下载地址

https://www.postgresql.org/download/

选择你需要下载的环境
第一个选择的是系统,第二个是选择你环境的发行版本。
这里我们系统以Linux中的Centos为例,所以这里我们选择的是Red Hat/Centos
image.png
选择你的系统环境
1.为选择需要安装的Postgresql版本,选择13,默认就会安装13下面最新的版本。
2.选择你系统版本号
3.选择系统的架构(一般都是默认的,且Centos系统都是x86架构)
4.就是查找出来,对应你选择的系统版本安装Postgresql需要执行的命令
image.png
下面是查询出来安装postgresql的命令,将下面的命令一一执行即可完成postgresql的安装。
下面带有# 号的为注释,不需要在系统中执行。

二:安装 postgresql-13

  1. # Install the repository RPM:这里的意思是“安装存储库RPM
  2. sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
  3. # Install PostgreSQL:这里的意思是“安装PostgreSQL
  4. sudo yum install -y postgresql13-server
  5. # Optionally initialize the database and enable automatic start:这里的意思是 “初始化数据库并设置为开机自动启动”
  6. sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
  7. sudo systemctl enable postgresql-13
  8. sudo systemctl start postgresql-13

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

  1. su - postgres
  2. psql

image.png
我们来设置密码后再退出

  1. postgres=# ALTER USER postgres WITH PASSWORD '所需要设置的密码';
  2. ALTER ROLE
  3. postgres=# \q

iShot2022-03-14 11.54.07.png

三:配置postgresql-13

配置远程连接,需要设置pg_hba.conf

  1. vim /var/lib/pgsql/13/data/pg_hba.conf

把local和host那三行内容给注释掉,在下面自己配置一条

  1. host all all 0.0.0.0/0 md5

iShot2022-03-14 11.57.50.png
wq 保存退出

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

  1. vim /var/lib/pgsql/13/data/postgresql.conf

放开listen_addresses和port的注释并把listen_addresses后的localhost改成*
iShot2022-03-14 12.02.16.png

wq保存退出

查看postgresql是否已经在运行

  1. [root@lizhiqi ~]# ps -ef | grep postgresql
  2. root 16271 16205 0 12:49 pts/0 00:00:00 grep --color=auto postgresql
  3. [root@lizhiqi ~]#

在服务器上放行postgresql 的端口

  1. 添加指定需要开放的端口:
  2. firewall-cmd --add-port=5432/tcp --permanent
  3. 重新载入添加的端口:
  4. firewall-cmd --reload
  5. 查询指定端口是否开启成功:
  6. firewall-cmd --query-port=5432/tcp

重启服务:

  1. systemctl restart postgresql-13

image.png

启动postgresql命令

  1. systemctl start postgresql

查看postgresql运行状态命令

  1. systemctl status postgresql