PostgreSQL11在CentOS7中的安装及配置

环境

1. 主机是Win10,ip地址为192.168.1.99(可改成你自己的);

 

2. postgres安装在VMware Workstion(版本是VMware-workstation-full-15.0.4-12990004)中的Centos(版本是版本为CentOS-7-x86_64-DVD-1810)虚拟机中。

 

3. Centos为最小化安装,语言添加了中文支持;在建系统时,新增了一个dbData分区,将其用于存放postgres的数据库数据;时区设为上海;ip使用IPv4(忽略了IPv6),ip地址在vmware NAT中的192.168.80网段,具体地址为192.168.80.20。这里的ip网段和ip地址可改成你自己的。

备注:未加特别说明,Centos都以root身份登录并操作的。

 

安装postgres

1. 首先更新repo,否则不能安装postgresql 10或11版本。

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

参见:https://www.postgresql.org/download/linux/redhat/

 

2. 安装包

# yum install postgresql11

# yum install postgresql11-server

 

配置CentOS

postgres安装结束后,在初始化数据库之前,还有些准备工作,目的是为了将postgres的数据存放在我前面建的dbData目录中。

1. 编辑root的.bash_profile文件

export PATH=/usr/pgsql-11/bin:$PATH #usr/pgsql-11/bin这个路径可以不用

export LD_LIBRARY_PATH=/usr/pgsql-11/lib #这句可以不加

export PGDATA=/data/postgres/11/data

 

2. 修改/usr/lib/systemd/system/postgresql-11.service

将“Environment=PGDATA=...”中的路径改成自己想要的路径,用以存放pg的数据:

Environment=PGDATA=/data/postgres/11/data/

 

3. 创建目录

mkdir -p /data/postgresql/11/data

chown postgres:postgres /data/postgresql/11/data

chmod 777 /data/postgresql/11/data

 

4. 重启centos

 

初始化postgres

# su postgres

$ /usr/pgsql-11/bin/initdb

接着有一串创建信息...

could not change directory to "/root": Permission denied
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /data/postgres/11/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Asia/Shanghai
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:
    /usr/pgsql-11/bin/pg_ctl -D /data/postgres/11/data -l logfile start

 创建成功

 

 

 

启动

# systemctl enable postgresql-11

# systemctl start postgresql-11

自此,在服务器端已可以进行postgres的操作了,比如

# su postgres
$ psql
could not change directory to "/root": Permission denied
psql (11.4)
Type "help" for help.

postgres=# \l
List of databases
Name       | Owner    | Encoding | Collate     | Ctype       | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
template0  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
           |          |          |             |             |  postgres=CTc/postgres
template1  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
           |          |          |             |             | postgres=CTc/postgres(3 rows)

 

 

 

 

收尾

1. 修改/data/postgres/11/data/pg_hba.conf,添加以下两行

# "local" is for Unix domain socket connections only

local all all trust

...

# IPv4 local connections:

host all all 192.168.1.0/24 md5

host all all 192.168.80.0/24 md5

第一行的作用是让处于192.168.1.0网段的客户端能够连接到pgserver上;第二行的使由于pgserver处于192.168.80.0网段,其网关为192.168.80.1,没有这行客户端无法连接上。

 

2. 修改postgresql.conf,如下

listen_addresses = '*'

这没什么好解释的。

在这个文件中,“port = 5432”是被注释掉的,也不用理会,pgserver默认就用了5432端口,除非你想改为其他的端口号。

 

3. 上面这个两个conf文件改好后,重启postgresql-11服务。

 

4. 到了这里,试图通过pgAdmin4去连接pgserver,仍可能会连接失败,这是因为Linux防火墙没有开放5432端口。通过firewall-cmd命令可以查看到我的接口位于public,可以执行下面的命令:

# firewall-cmd --get-active-zones

public

interfaces: ens33

系统显示出虚拟机中的网卡位于public区域,接着可以执行下面的命令:

# firewall-cmd --zone=public --add-port=5432/tcp --permanent

开放了5432端口后,接着须重启防火墙,为了让配置生效,可以执行下面的命令:

# firewall-cmd --reload

 

5. 接着,试着用我的Win10机器上的pgAdmin4连接postgres,发现需要postgres的口令,于是

# su - postgres

$ psql -c "alter user postgres with password 'password'"

提示“could not change directory to "/root": Permission denied”

于是,我在psql下执行:

postgres=# \password

Enter new password:

Enter it again:

按提示输入两次口令后,再去连接pgadmin4,成了。

 

至此,postgres11的基本安装与配置就完工了,但其实后期还有很多事情要做……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值