CentOS7下使用RPM安装PostgreSQL10

安装

安装依赖

1
yum -y install libicu libxslt-devel

准备RPM包

下载地址

1
2
3
4
postgresql10-10.4-1PGDG.rhel7.x86_64.rpm
postgresql10-contrib-10.4-1PGDG.rhel7.x86_64.rpm
postgresql10-libs-10.4-1PGDG.rhel7.x86_64.rpm
postgresql10-server-10.4-1PGDG.rhel7.x86_64.rpm

安装

1
rpm -ivh *.rpm

初始化数据库

默认数据目录: /var/lib/pgsql/10/data/

1
/usr/pgsql-10/bin/postgresql-10-setup initdb

自定义目录

1
2
3
4
5
mkdir -p /opt/PostgreSQL/data
chmod 755 -R /opt/PostgreSQL/data
chown postgres:postgres -R /opt/PostgreSQL
su postgres
/usr/pgsql-10/bin/initdb --encoding=UTF-8 --local=zh_CN.UTF8 --username=postgres --pwprompt --pgdata=/opt/PostgreSQL/data

修改service环境变量

修改Environment:PGDATA的数据目录为/opt/PostgreSQL/data

1
2
vim /usr/lib/systemd/system/postgresql-10.service
systemctl daemon-reload

修改postgres用户的环境变量PGDATA

1
2
su postgres
vi ~/.bash_profile

启动

1
2
3
4
5
6
# 启动
systemctl start postgresql-10.service
# 开机启动
systemctl enable postgresql-10.service
# 关闭开机启动
systemctl disable postgresql-10.service

登录

1
2
su postgres
psql -U postgres

查看当前数据目录

1
2
3
4
show data_directory;
----------------------
/opt/PostgreSQL/data
(1 row)

修改密码

1
2
postgres=# \password
ALTER USER postgres WITH PASSWORD 'xxxxx';

数据库登录权限设置

$PGDATA/pg_hba.conf 权限相关配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
#host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5
# IPv6 local connections:
#host all all ::1/128 trust
host all all ::0/0 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication all trust
#host replication all 127.0.0.1/32 trust
#host replication all ::1/128 trust
host replication repl slave trust

设置trust,本地可以使用psql -U postgres直接登录服务器
设置peer,本地可以使用psql -h 127.0.0.1 -d postgres -U postgres直接登录服务器
设置password,使用用户名密码登录

$PGDATA/postgresql.conf 数据库相关配置

1
2
listen_addresses = '*'
posrt = 5432

设置监听任意IP, 允许任意ip连接数据库
更多权限说明,见官方文档