Ubuntu24 安装 PostgreSQL

📅 2026/8/15 10:58:44
Ubuntu24 安装 PostgreSQL
安装更新 apt 并安装 postgresql我这里安装的是最新版sudoaptupdatesudoaptinstallpostgresql安装完成后PostgreSQL 服务将自动启动。可以通过如下的命令查看服务是否启动sudosystemctl status postgresql查看安装的版本psql--version允许远程访问 PostgreSQLsudovim/etc/postgresql/16/main/postgresql.conf修改下面的内容需要打开注释listen_addresses*默认值 localhost如果未修改默认通常是 listen_addresses ‘localhost’。这表示 PostgreSQL 只监听本地回环地址只能从本机连接完全无法接受来自其他电脑或应用的网络请求。修改为 ‘*’ 后打破了本地限制允许远程主机访问。sudovim/etc/postgresql/16/main/pg_hba.conf文件末尾添加如下一行内容hostall all0.0.0.0/0 md5允许所有 IP 地址通过密码认证访问 PostgreSQL设置 PostgreSQL 用户密码sudo-upostgres psql# ** 请替换为你想要的密码ALTERUSERpostgres WITH ENCRYPTED PASSWORD**;完成后输入\q退出 PostgreSQL 命令行。重启 postgres sql 服务sudosystemctl restart postgresql查看 postgresql 状态sudosystemctl status postgresql