Ubuntu20.04 PostgreSQL 14 安装配置记录

发布时间 2023-12-11 17:23:04作者: 疯子110

PostgreSQL 名称来源
It was originally named POSTGRES, referring to its origins as a successor to the Ingres database developed at the University of California, Berkeley.

In 1996, the project was renamed to PostgreSQL to reflect its support for SQL.

PostgreSQL 的发音为 [ˈpəʊsɡreˈsɪkl], 中间部分类似于 progress 的发音

安装
参考官方安装说明 https://www.postgresql.org/download/linux/ubuntu/

# 创建软件源
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# 添加key
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# 安装
sudo apt update
sudo apt install postgresql
# 检查
sudo systemctl status postgresql
# 查看端口
sudo netstat -lnp
1
2
3
4
5
6
7
8
9
10
11
安装后, 系统中会增加一个 postgres 用户, 注意哦, 这个用户是可以登录的. 建议给这个用户设个密码

$ more /etc/passwd
...
postgres:x:113:121:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash

这个用户可以直接访问 postgresql

$ sudo su postgres
[sudo] password for milton:
postgres@ubuntu:/home/milton$ psql
psql (14.2 (Ubuntu 14.2-1.pgdg20.04+1))
Type "help" for help.

postgres=#
postgres-# \q
postgres@ubuntu:/home/milton$

配置
查看数据库列表
sudo -u postgres psql -l
创建用户
# 使用 postgres 用户
createuser --interactive
# 或者 sudo
sudo -u postgres createuser --interactive

默认情况下, postgresql 会使用与linux用户同名的用户登录, 所以如果创建了用户为 milton, 还需要再创建一个名为 milton 的 linux 用户.

创建数据库
# 使用 postgres 用户
createdb milton
# 或者 sudo
sudo -u postgres createdb milton
# 指定用户
sudo -u postgres createdb testdb -O postgres

主配置
对应的配置文件在 /etc/postgresql//main, 当前的版本是14, 路径是 /etc/postgresql/14/main/postgresql.conf

sudo vi /etc/postgresql/14/main/postgresql.conf
主配置文件说明 https://www.postgresql.org/docs/14/runtime-config-connection.html

服务IP listen_addresses

# 监听所有地址
listen_addresses = '*'
# 监听指定地址
listen_addresses = '192.168.10.20'

服务端口 port

port = 5432
密码加密方式 password_encryption

password_encryption = scram-sha-256 # scram-sha-256 or md5
用户名命名空间 db_user_namespace, 如果设置为on, 用户创建时可以使用 username@dbname 这样的格式, 用于与数据库绑定. 这时候依然可以创建全局用户, 但是连接时客户端必须加上 @

db_user_namespace = off
基于主机的认证配置
配置文件 pg_hba.conf, 配置说明 https://www.postgresql.org/docs/14/auth-pg-hba-conf.html

客户端认证由配置文件控制, 通常为名为 pg_hba.conf 的文件, 存储在集群的数据目录(HBA 代表 host-based authentication 的缩写). 当数据目录初始化时, 会生成一个默认的 pg_hba.conf 文件. 可以通过修改主配置文件, 将文件放到其他路径.

pg_hba.conf 文件通常的格式是按行组织的文本记录

使用#号标识注释
如果一行未结束需要换行, 使用\符号.
每行记录由一些空格或tab分隔的字段组成. 如果字段包含空格, 需要用双引号包围.
每行记录指定了: 连接类型, 客户端IP范围, 数据库名, 用户名, 验证方式.
匹配的第一个记录(匹配连接类型+客户端地址+数据库+用户名)将用于验证
没有缺省或再次验证, 只要一个记录被选中, 那么验证就只用这个记录处理, 如果没有命中的记录, 就返回拒绝.
初始配置示例
# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database superuser can access the database using some other method.
# Noninteractive access to all databases is required during automatic
# maintenance (custom daily cronjobs, replication, and similar tasks).
#
# Database administrative login by Unix domain socket
local all postgres peer

# TYPE DATABASE USER ADDRESS METHOD

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

记录格式说明
记录可以是这些格式

local database user auth-method [auth-options]
host database user address auth-method [auth-options]
hostssl database user address auth-method [auth-options]
hostnossl database user address auth-method [auth-options]
hostgssenc database user address auth-method [auth-options]
hostnogssenc database user address auth-method [auth-options]
host database user IP-address IP-mask auth-method [auth-options]
hostssl database user IP-address IP-mask auth-method [auth-options]
hostnossl database user IP-address IP-mask auth-method [auth-options]
hostgssenc database user IP-address IP-mask auth-method [auth-options]
hostnogssenc database user IP-address IP-mask auth-method [auth-options]

连接方式

local 使用本机Unix-domain sockets, 如果没有local开头的记录, 则不允许用Unix-domain sockets连接
host 使用TCP/IP连接, 包含SSL和GSSAPI方式
hostssl TCP/IP + 使用SSL
hostnossl TCP/IP + 不使用SSL
hostgssenc TCP/IP + GSSAPI 加密
hostnogssenc TCP/IP + 不使用 GSSAPI 加密
数据库, 指定匹配的数据库

数据库名 指定数据库, 多个数据库使用逗号连接
all 匹配所有
sameuser 与此数据库同名的用户, 必须是这个用户
samerole 与此数据库同名的role, 用户必须属于这个role
samegroup 以废弃
replication
@ 可以用@号指定文件
用户, 指定匹配的用户

用户名 指定的用户, 多个用户用+号连接
all 所有用户
@ 可以用@号指定文件
客户端地址

172.20.143.89/32 IPv4地址或范围
172.20.1.1/255.0.0.0 IPv4地址范围的另一种格式
fe80::7a31:c1ff:0000:0000/96 IPv6地址或范围
all 所有地址
samenet 同一子网的地址
samehost 当前主机的所有地址
.example.com 域名通配
验证方式

trust 无条件通过
reject 直接拒绝
scram-sha-256 使用SCRAM-SHA-256验证
md5 使用 Perform SCRAM-SHA-256 或 MD5 验证
password 使用未加密的密码验证, 注意这种方式下, 密码在网络中是明文传输
gss 使用 GSSAPI 验证, 仅适用于 TCP/IP 连接.
sspi 使用 SSPI 验证, 仅适用于 Windows
ident 通过ident服务器, 获取当前客户端操作系统用户名, 并与请求的数据库用户名进行校验, 仅适用于 TCP/IP 连接.
peer 从操作系统获取用户名, 仅适用于 local 方式的连接
ldap Authenticate using an LDAP server.
radius Authenticate using a RADIUS server
cert 使用 SSL 客户端证书进行验证
pam 使用操作系统提供的 Pluggable Authentication Modules (PAM) 服务进行验证
bsd 使用操作系统提供的 BSD Authentication service 进行验证
验证选项

根据不同的验证方式提供的选项
常用命令
修改用户口令
postgres=# ALTER USER postgres WITH PASSWORD 'postgres';
1
待更新补充
————————————————
版权声明:本文为CSDN博主「IOsetting」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/michaelchain/article/details/122952151