欢迎阅读本教程!本文将带你一步步在Ubuntu系统上安装最新版的PostgreSQL 18数据库。无论你是初学者还是经验丰富的开发者,都能轻松跟上。
首先,打开终端并更新软件包列表:
sudo apt updatesudo apt upgrade -y
安装一些必要的依赖包:
sudo apt install wget ca-certificates
PostgreSQL 18可能尚未进入默认Ubuntu仓库,因此我们需要添加官方APT仓库。首先导入仓库的GPG密钥:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
然后添加仓库(以Ubuntu 22.04 Jammy为例,其他版本请替换相应代号):
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
更新包列表并安装PostgreSQL 18:
sudo apt updatesudo apt install postgresql-18
此命令将安装PostgreSQL 18服务器和客户端工具。
安装完成后,PostgreSQL服务会自动启动。检查状态:
sudo systemctl status postgresql
如果未运行,手动启动并设置开机自启:
sudo systemctl start postgresqlsudo systemctl enable postgresql
默认情况下,PostgreSQL创建一个名为postgres的系统用户。切换到该用户并进入PostgreSQL提示符:
sudo -i -u postgrespsql
你可以设置postgres用户的密码:
\password postgres
退出psql:\q,然后退出postgres用户:exit。
如果需要从其他机器连接数据库,编辑PostgreSQL配置文件:
sudo nano /etc/postgresql/18/main/postgresql.conf
找到listen_addresses行,修改为:
listen_addresses = "*"
然后编辑客户端认证文件:
sudo nano /etc/postgresql/18/main/pg_hba.conf
添加一行允许远程连接,例如允许所有IP使用MD5认证:
host all all 0.0.0.0/0 md5
保存并重启服务:
sudo systemctl restart postgresql
别忘了配置防火墙允许5432端口:
sudo ufw allow 5432/tcp
可以创建一个测试数据库并连接:
sudo -u postgres createdb testdbsudo -u postgres psql -d testdb
在psql中执行\l查看所有数据库,应该能看到testdb。
至此,你已经成功在Ubuntu上安装了PostgreSQL 18。记得定期备份数据库,并关注官方安全更新。
本教程围绕Ubuntu安装PostgreSQL 18、PostgreSQL 18教程、Linux数据库安装和PostgreSQL配置等关键词展开,希望对你有帮助。
本文由主机测评网于2026-03-10发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:http://www.vpshk.cn/20260330085.html