Centos 7 安装 PostgreSQL 12[step-by-step]

本指南将引导您完成在 CentOS 7 / CentOS 8 Linux 服务器上安装 PostgreSQL 12的步骤。 PostgreSQL 是基于 POSTGRES 4.2 的对象关系数据库管理系统。 开发人员和数据库管理员可以使用 PostgreSQL 12。

PostgreSQL项目为最常见的发行版提供了所有受支持版本的软件包的存储库。 支持的发行版包括所有 Red Hat系列,其中包括 CentOS,Fedora,Scientific Linux,Oracle Linux和Red Hat Enterprise Linux。

对于Ubuntu用户,请查看:在 Ubuntu上 安装PostgreSQL 12

使用以下步骤在 CentOS 8 / CentOS 7上安装 PostgreSQL 12。

1. 将PostgreSQL Yum存储库添加到CentOS 7 / CentOS 8

PostgreSQL Yum 存储库将与您的常规系统和补丁程序管理集成,并在 PostgreSQL 的整个支持期内为所有受支持的 PostgreSQL 版本提供自动更新。

可以通过运行以下命令将其添加到 CentOS 系统中:

CentOS 8:

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

CentOS 7:

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

2. 在CentOS 8 / CentOS 7上安装PostgreSQL 12

添加YUM存储库后,我们可以使用以下命令在CentOS 7/8上安装PostgreSQL 12。

sudo yum -y install epel-release yum-utils
sudo yum-config-manager --enable pgdg12
sudo yum install postgresql12-server postgresql12

数据库的主要配置文件是:/var/lib/pgsql/12/data/postgresql.conf

Now Install PostgreSQL 12 on CentOS 7 / CentOS 8:
sudo yum clean all
sudo yum makecache fast
sudo yum -y install postgresql12-server postgresql12

2.1 PostgreSQL数据路径更改

如果您希望将数据放在(例如)/ pgdata / 10 / data中,请创建具有良好权限的目录:

# mkdir -p /data/pgdata/
# chown -R postgres:postgres /data/pgdata

然后,自定义systemd服务:

# systemctl edit postgresql-12.service

添加以下内容:

[Service]
Environment=PGDATA=/data/pgdata/

3. 初始化并启动数据库服务

安装后,需要先进行数据库初始化,然后才能启动服务。

sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
sudo systemctl enable --now postgresql-12

Confirm that the service is started without any errors.

$ systemctl status postgresql-12
● postgresql-12.service - PostgreSQL 12 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2019-09-19 18:50:10 UTC; 39s ago
     Docs: https://www.postgresql.org/docs/12/static/
  Process: 10647 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 10652 (postmaster)
   CGroup: /system.slice/postgresql-12.service
           ├─10652 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
           ├─10654 postgres: logger   
           ├─10656 postgres: checkpointer   
           ├─10657 postgres: background writer   
           ├─10658 postgres: walwriter   
           ├─10659 postgres: autovacuum launcher   
           ├─10660 postgres: stats collector   
           └─10661 postgres: logical replication launcher   

Sep 19 18:50:10 cent7.novalocal systemd[1]: Starting PostgreSQL 12 database server...
Sep 19 18:50:10 cent7.novalocal postmaster[10652]: 2019-09-19 18:50:10.207 UTC [10652] LOG:  starting PostgreSQL 12beta4 on x86_64-pc-lin... 64-bit
Sep 19 18:50:10 cent7.novalocal postmaster[10652]: 2019-09-19 18:50:10.209 UTC [10652] LOG:  listening on IPv6 address "::1", port 5432
Sep 19 18:50:10 cent7.novalocal postmaster[10652]: 2019-09-19 18:50:10.209 UTC [10652] LOG:  listening on IPv4 address "127.0.0.1", port 5432
Sep 19 18:50:10 cent7.novalocal postmaster[10652]: 2019-09-19 18:50:10.214 UTC [10652] LOG:  listening on Unix socket "/var/run/postgresq...L.5432"
Sep 19 18:50:10 cent7.novalocal postmaster[10652]: 2019-09-19 18:50:10.229 UTC [10652] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
Sep 19 18:50:10 cent7.novalocal postmaster[10652]: 2019-09-19 18:50:10.254 UTC [10652] LOG:  redirecting log output to logging collector process
Sep 19 18:50:10 cent7.novalocal postmaster[10652]: 2019-09-19 18:50:10.254 UTC [10652] HINT:  Future log output will appear in directory "log".
Sep 19 18:50:10 cent7.novalocal systemd[1]: Started PostgreSQL 12 database server.
Hint: Some lines were ellipsized, use -l to show in full.

如果您有正在运行的防火墙服务,并且远程客户端应连接到数据库服务器,则允许PostgreSQL服务。

sudo firewall-cmd --add-service=postgresql --permanent
sudo firewall-cmd --reload

4. Set PostgreSQL admin user’s password

Set PostgreSQL admin user
$ sudo su - postgres 
-bash-4.2$ psql -c "alter user postgres with password 'StrongPassword'" 
ALTER ROLE

然后用命令退回:

-bash-4.2$ exit

5. Enable remote access (Optional)

Edit the file /var/lib/pgsql/12/data/postgresql.conf and set Listen address to your server IP address or * for all interfaces.

listen_addresses = '192.168.10.10'  # OR '*'

Also set PostgreSQL to accept remote connections

$ sudo vim /var/lib/pgsql/12/data/pg_hba.conf
# Accept from anywhere
host all all 0.0.0.0/0 md5

# Accept from trusted subnet
host all all 192.168.18.0/24 md5
# If you want to allow non-local connections, you need to add more
# "host" records.  In that case you will also need to make PostgreSQL
# listen on a non-local interface via the listen_addresses
# configuration parameter, or via the -i or -h command line switches.



# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             0.0.0.0/0(这~~~)     trust
# 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

Restart database service after committing the change.

sudo systemctl restart postgresql-12

PostgreSQL 12 has been installed on CentOS 7 / CentOS 8 Linux system. Spare some minutes to go through PostgreSQL 12 documentation.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值