在Ubuntu 22.04上安装PostgreSQL 15
PostgreSQL
作为一种现代化的数据库,在市场份额上逐年上升。本人长期使用MySQL 5.6版本,也想体验一把现代数据库的优越性。
本文所写的环境是Ubuntu 22.04,是在Window 11环境下的子系统WSL(Windows Sub System),关于如何安装WSL,可参考微软的官方文档:适用于 Linux 的 Windows 子系统文档 | Microsoft Learn。
为了使能在本机(Win 11)上访问Ubuntu下的PostgreSQL,需要配置其相关的参数,包括账号用户名及网络。以下是本人在实践过程中的配置过程。
使用Apt Repository安装
参考文档: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'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql
复制,粘贴到终端中,按回车输入账号密码,然后就开始执行脚本。执行完以上脚本之后,使用psql --version
就能看到当前PostgreSQL的版本号了,这里我的版本号未15.4
。
$ psql --version
psql (PostgreSQL) 15.4 (Ubuntu 15.4-1.pgdg22.04+1)
然后我们再查看一下PostgreSQL的安装路径,再终端中输入命令whereis postgresql
,就能看到PostgreSQL安装的路径了,其中配置文件是在/etc/postgresql/15/main
中保存着。
$ whereis postgresql
postgresql: /usr/lib/postgresql /etc/postgresql /usr/share/postgresql
账号密码
安装完之后使用sudo -u postgres psql
,命令能进入数据库交互界面,类似mysql中的mysql
命令。此处对于该命令的立即,是本机使用postgres
账号登录PostgreSQL是不需要密码的。此处需要说明的是,由于使用mysql
命令时会将命令放在参数前面,所以在尝试使用sudo psql -u postgres
时发现无法正常运行,出现了以下的错误信息:
$ sudo psql -u postgres
/usr/lib/postgresql/15/bin/psql: invalid option -- 'u'
psql: hint: Try "psql --help" for more information.
使用psql
能对本地的数据库进行一些操作,但是对于有放置于外网访问的数据库,则需要配置一定的账号权限等相关配置。其中对于客户端验证的配置放置在pg_hba.conf
文件中。hba代表的意思是host-based authentication,即基于主机的身份验证。其中各个参数的配置说明,可参考官方参考文档 。
查看pg_hba.conf
文件,其中有几条已经配置好的内容。
/etc/postgresql/15/main$ sudo cat pg_hba.conf
# 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
# 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
其中,第一条命令提示不要禁用。增加配置,以让内网用户访问。
在服务器上使用route -n
查看路由表,能看到网卡的关网
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.28.112.1 0.0.0.0 UG 0 0 0 eth0
172.28.112.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0
上述Gateway下面的172.28.112.1
即是网关地址,也是本人所在子系统的主机虚拟网卡地址。在Windows命令行中输入ipconfig /all
,能查看到WSL网卡的IP地址。
ipconfig /all
以太网适配器 vEthernet (WSL):
连接特定的 DNS 后缀 . . . . . . . :
描述. . . . . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter
物理地址. . . . . . . . . . . . . : 00-15-5D-EB-E9-C7
DHCP 已启用 . . . . . . . . . . . : 否
自动配置已启用. . . . . . . . . . : 是
本地链接 IPv6 地址. . . . . . . . : fe80::e3d9:c85a:2bfd:4539%50(首选)
IPv4 地址 . . . . . . . . . . . . : 172.28.112.1(首选)
子网掩码 . . . . . . . . . . . . : 255.255.240.0
默认网关. . . . . . . . . . . . . :
DHCPv6 IAID . . . . . . . . . . . : 838866269
DHCPv6 客户端 DUID . . . . . . . : 00-01-00-01-2C-36-DC-B9-A0-36-BC-09-5A-35
TCPIP 上的 NetBIOS . . . . . . . : 已启用
根据网关地址及掩码,可以得到IP地址范围。
host all all 172.28.112.0/20 scram-sha-256
第二步,编辑postgresl.conf
文件,找到listen_addresses
,该配置参数是数据库默认监听的地址,默认是注释掉的,且其默认值为localhost
,增加listen_addresses
的值,这里为方便,直接使用*
号,表示所有地址都监听。
默认情况下PostgreSQL是没有密码的,远程也没法访问,在服务器使用命令sudo -u postgres psql
进入命令交互控制台,修改默认账号postgres
的密码,使用ALTER USER
(参考文档)语句进行修改,注意结束时的分号。
alter user postgres with password 'PostgreSQL';
通过以上配置之后,重启PostgreSQL服务器。
$ sudo systemctl restart postgresql
使用客户端,列如DataGrip进行访问,账号为postgres
,密码为PostgreSQL
,连接地址为172.28.121.31
,端口号为5432
,即可连接成功。
在Ubuntu 22.04上安装PostgreSQL 15
PostgreSQL
作为一种现代化的数据库,在市场份额上逐年上升。本人长期使用MySQL 5.6版本,也想体验一把现代数据库的优越性。
本人使用的Ubuntu是Window 11环境下的子系统WSL(Windows Sub System),关于如何安装WSL,可参考微软的官方文档:适用于 Linux 的 Windows 子系统文档 | Microsoft Learn。
为了使能在本机上访问Ubunt下的PostgreSQL,需要配置其相关的参数,包括账号用户名及网络。以下是本人在实践过程中的配置过程。
使用Apt Repository安装
参考文档: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'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql
复制,粘贴到终端中,按回车输入账号密码,然后就开始执行脚本。执行完以上脚本之后,使用psql --version
就能看到当前PostgreSQL的版本号了,这里我的版本号未15.4
。
$ psql --version
psql (PostgreSQL) 15.4 (Ubuntu 15.4-1.pgdg22.04+1)
然后我们再查看一下PostgreSQL的安装路径,再终端中输入命令whereis postgresql
,就能看到PostgreSQL安装的路径了,其中配置文件是在/etc/postgresql/15/main
中保存着。
$ whereis postgresql
postgresql: /usr/lib/postgresql /etc/postgresql /usr/share/postgresql
账号密码
安装完之后使用sudo -u postgres psql
,命令能进入数据库交互界面,类似mysql中的mysql
命令。此处对于该命令的立即,是本机使用postgres
账号登录PostgreSQL是不需要密码的。此处需要说明的是,由于使用mysql
命令时会将命令放在参数前面,所以在尝试使用sudo psql -u postgres
时发现无法正常运行,出现了以下的错误信息:
$ sudo psql -u postgres
/usr/lib/postgresql/15/bin/psql: invalid option -- 'u'
psql: hint: Try "psql --help" for more information.
使用psql
能对本地的数据库进行一些操作,但是对于有放置于外网访问的数据库,则需要配置一定的账号权限等相关配置。其中对于客户端验证的配置放置在pg_hba.conf
文件中。hba代表的意思是host-based authentication,即基于主机的身份验证。其中各个参数的配置说明,可参考官方参考文档 。
查看pg_hba.conf
文件,其中有几条已经配置好的内容。
/etc/postgresql/15/main$ sudo cat pg_hba.conf
# 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
# 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
其中,第一条命令提示不要禁用。增加配置,以让内网用户访问。
在服务器上使用route -n
查看路由表,能看到网卡的关网
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.28.112.1 0.0.0.0 UG 0 0 0 eth0
172.28.112.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0
上述Gateway下面的172.28.112.1
即是网关地址,也是本人所在子系统的主机虚拟网卡地址。在Windows命令行中输入ipconfig /all
,能查看到WSL网卡的IP地址。
ipconfig /all
以太网适配器 vEthernet (WSL):
连接特定的 DNS 后缀 . . . . . . . :
描述. . . . . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter
物理地址. . . . . . . . . . . . . : 00-15-5D-EB-E9-C7
DHCP 已启用 . . . . . . . . . . . : 否
自动配置已启用. . . . . . . . . . : 是
本地链接 IPv6 地址. . . . . . . . : fe80::e3d9:c85a:2bfd:4539%50(首选)
IPv4 地址 . . . . . . . . . . . . : 172.28.112.1(首选)
子网掩码 . . . . . . . . . . . . : 255.255.240.0
默认网关. . . . . . . . . . . . . :
DHCPv6 IAID . . . . . . . . . . . : 838866269
DHCPv6 客户端 DUID . . . . . . . : 00-01-00-01-2C-36-DC-B9-A0-36-BC-09-5A-35
TCPIP 上的 NetBIOS . . . . . . . : 已启用
根据网关地址及掩码,可以得到IP地址范围。
host all all 172.28.112.0/20 scram-sha-256
第二步,编辑postgresl.conf
文件,找到listen_addresses
,该配置参数是数据库默认监听的地址,默认是注释掉的,且其默认值为localhost
,增加listen_addresses
的值,这里为方便,直接使用*
号,表示所有地址都监听。
默认情况下PostgreSQL是没有密码的,远程也没法访问,在服务器使用命令sudo -u postgres psql
进入命令交互控制台,修改默认账号postgres
的密码,使用ALTER USER
(参考文档)语句进行修改,注意结束时的分号。
alter user postgres with password 'PostgreSQL';
通过以上配置之后,重启PostgreSQL服务器。
$ sudo systemctl restart postgresql
使用客户端,如下所示使用DataGrip进行访问,账号为postgres
,密码为PostgreSQL
,连接地址为172.28.121.31
,端口号为5432
,即可连接成功。