PostgreSQL 快速安装

安装

官方下载地址:https://www.postgresql.org/download/

在页面上选择适合的版本,按照说明下载安装即可。

我选择的版本是 CentOS7 Postgresql10

shell
1
2
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install -y postgresql10-server

初始化

shell
1
2
3
4
5
6
# 初始化数据库实例
/usr/pgsql-10/bin/postgresql-10-setup initdb
# 开机自启动
systemctl enable postgresql-10
# 手动启动
systemctl start postgresql-10

默认路径

安装目录:/usr/pgsql-10

数据库实例:/var/lib/pgsql/10/data

连接数据库

默认情况下,数据库只允许在本地postgres用户免密登录。

实际使用还需要设置用户密码:

shell
1
2
3
4
5
6
7
8
9
10
# 切换到postgres用户
su postgres
# 连接到本地数据库
psql
# 修改用户密码
alter user postgres with password 'postgres';
# 断开连接
\q
# 登出postgres用户
exit

然后改客户端认证配置文件:

/var/lib/pgsql/10/data/pg_hba.conf
1
2
3
4
5
# TCP/IP连接协议 数据库 用户  网络掩码    加密方式
host all all 0.0.0.0/0 md5

# 详细配置可参考官方文档
# https://www.postgresql.org/docs/10/client-authentication.html

同时还要修改监听配置:

/var/lib/pgsql/10/data/postgresql.conf
1
2
3
4
5
# 找到 listen_addresses = 'localhost' 修改为 '*'
listen_addresses = '*'

# 详细配置可参考官方文档
# https://www.postgresql.org/docs/10/runtime-config.html

再重启数据库

shell
1
systemctl restart postgresql-10

现在可以在客户端连接数据库了

shell
1
2
psql -h 127.0.0.1 -U postgres
# 密码:postgres