温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

postgresql如何安装配置

发布时间:2026-09-07 02:50:38 来源:亿速云 阅读:120 作者:小樊 栏目:数据库

下面按常见使用场景给你讲 PostgreSQL 的安装与基础配置,分为 Linux(Ubuntu/CentOS)、Windows、macOS,以及基础配置(用户、数据库、远程访问)。


一、Linux 安装

1️⃣ Ubuntu / Debian

sudo apt update
sudo apt install postgresql postgresql-contrib

安装后自动创建:

  • 系统用户:postgres
  • 数据库服务:postgresql

启动 / 查看状态:

sudo systemctl start postgresql
sudo systemctl enable postgresql
sudo systemctl status postgresql

2️⃣ CentOS / Rocky / Alma

sudo dnf install postgresql-server postgresql-contrib
sudo postgresql-setup --initdb
sudo systemctl start postgresql
sudo systemctl enable postgresql

二、Windows 安装

  1. 官网下载安装包
    https://www.postgresql.org/download/windows/
  2. 运行安装向导
  3. 设置:
    • 超级用户密码(postgres)
    • 端口(默认 5432)
    • 区域(默认即可)
  4. 安装完成后:
    • 使用 pgAdmin 管理
    • 或命令行:psql -U postgres

三、macOS 安装

使用 Homebrew(推荐)

brew install postgresql
brew services start postgresql

连接测试:

psql postgres

四、基础配置(重要)

1️⃣ 切换到 postgres 用户

sudo -i -u postgres

2️⃣ 登录数据库

psql

3️⃣ 修改 postgres 密码

ALTER USER postgres PASSWORD 'your_password';

五、创建用户和数据库

-- 创建用户
CREATE USER myuser WITH PASSWORD 'mypassword';

-- 创建数据库
CREATE DATABASE mydb OWNER myuser;

-- 授权
GRANT ALL PRIVILEES ON DATABASE mydb TO myuser;

六、允许远程连接(常见需求)

1️⃣ 修改 postgresql.conf

路径示例:

/etc/postgresql/15/main/postgresql.conf

修改:

listen_addresses = '*'

2️⃣ 修改 pg_hba.conf

host    all    all    0.0.0.0/0    md5

或者指定 IP:

host    all    all    192.168.1.0/24    md5

3️⃣ 重启服务

sudo systemctl restart postgresql

七、常用命令速查

# 启动
sudo systemctl start postgresql

# 停止
sudo systemctl stop postgresql

# 登录
psql -U username -d dbname -h host

# 查看数据库
\l

# 查看表
\dt

八、常见问题

✅ 连接被拒绝

  • 检查服务是否启动
  • 检查 listen_addresses
  • 检查防火墙(5432 端口)

✅ peer authentication failed

sudo -u postgres psql

如果你告诉我:

  • 操作系统
  • 是否用于 生产 / 开发
  • 是否需要 Docker / 主从 / 备份

我可以给你更针对性的配置方案。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI
助
手