Ubuntu 22.04 上安装 PostgreSQL(使用官方 APT 源)

Ubuntu 22.04 上安装 PostgreSQL(使用官方 APT 源)

步骤 1:更新系统

bash 复制代码
sudo apt update
sudo apt upgrade -y

步骤 2:添加 PostgreSQL 官方仓库

bash 复制代码
# 安装仓库管理工具
sudo apt install wget ca-certificates gnupg lsb-release -y

# 导入 GPG 密钥
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /usr/share/keyrings/postgresql.gpg

# 添加仓库
echo "deb [signed-by=/usr/share/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list

步骤 3:安装 PostgreSQL

bash 复制代码
sudo apt update
sudo apt install postgresql postgresql-contrib -y
  • postgresql 是主程序。
  • postgresql-contrib 包括一些有用的扩展模块(如 uuid-ossppg_stat_statements 等)。

TIPS: 这里网络可能会比较卡,需要用到代理服务。

复制代码
export http_proxy="http://your-proxy-host:port"
export https_proxy="http://your-proxy-host:port"

步骤 4:检查服务状态

bash 复制代码
sudo systemctl status postgresql

如果未启动,可以执行:

bash 复制代码
sudo systemctl start postgresql
sudo systemctl enable postgresql

配置远程访问PostgreSQL,以及设置管理员密码

默认情况下,postgres 用户在数据库中并没有设置密码,也无法用密码直接登录。


🔐 默认用户密码:

  • Ubuntu 系统中会创建一个同名的 Linux 用户 postgres
  • 你可以通过切换到这个 Linux 用户来访问 PostgreSQL:
bash 复制代码
sudo -i -u postgres
psql

这是一种基于 Unix socket 和系统身份验证 的方式,不需要密码。

  1. 切换到 postgres 用户:

    bash 复制代码
    sudo -i -u postgres
  2. 进入 PostgreSQL:

    bash 复制代码
    psql
  3. 在 PostgreSQL 手动指定密码:

    sql 复制代码
    ALTER USER postgres WITH PASSWORD 'your_strong_password';
  4. 退出:

    sql 复制代码
    \q

🌐 配置远程连接,用密码登录

你还需要修改两个文件(一般位于 /etc/postgresql/14/main/,版本号视实际情况而定):

1. 修改 postgresql.conf

bash 复制代码
sudo nano /etc/postgresql/14/main/postgresql.conf

找到并修改:

conf 复制代码
listen_addresses = '*'

2. 修改 pg_hba.conf

bash 复制代码
sudo nano /etc/postgresql/14/main/pg_hba.conf

把:

conf 复制代码
local   all             postgres                                peer

改为:

conf 复制代码
local   all             postgres                                md5

并添加远程访问规则,例如:

conf 复制代码
host    all             all             0.0.0.0/0               md5

注意:可根据实际网段限制 IP 范围,比如 192.168.1.0/24

3. 重启 PostgreSQL 服务

bash 复制代码
sudo systemctl restart postgresql

✅ 检查密码登录

你可以用 psql 或远程客户端(如 DBeaver、pgAdmin、JDBC)来测试:

bash 复制代码
psql -h 127.0.0.1 -U postgres -W

可能遇到的问题(我自己遇到的)

这个错误:

复制代码
ERROR: column "datlastsysoid" does not exist

是由于 Navicat(或其他某些客户端)使用了旧版本的 PostgreSQL 系统元数据结构 ,而 PostgreSQL 14+ 已经移除了 datlastsysoid 字段,导致访问系统视图时报错。

升级Navicat版本,或者使用其他的链接工具即可。

相关推荐
Johny_Zhao15 小时前
OpenClaw安装部署教程
linux·人工智能·ai·云计算·系统运维·openclaw
赵渝强老师1 天前
【赵渝强老师】PostgreSQL中表的碎片
数据库·postgresql
chlk1232 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑2 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件2 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
深紫色的三北六号3 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash3 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI3 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行4 天前
Linux和window共享文件夹
linux
木心月转码ing4 天前
WSL+Cpp开发环境配置
linux