返回顶部
分享到

Mac OS上安装PostgreSQL完整图文教程

PostgreSQL 来源:互联网 作者:佚名 发布时间:2026-01-11 17:07:49 人浏览
摘要

macOS 上安装 PostgreSQL(完整图文教程) 适用于macOS 12+(Monterey / Ventura / Sonoma / Sequoia),支持Apple Silicon(M1/M2/M3)和Intel 推荐使用Homebrew(最简单、最常用) 一、推荐方式:使用 Homebrew 安装(

macOS 上安装 PostgreSQL(完整图文教程)

适用于 macOS 12+(Monterey / Ventura / Sonoma / Sequoia),支持 Apple Silicon(M1/M2/M3) 和 Intel
推荐使用 Homebrew(最简单、最常用)

一、推荐方式:使用 Homebrew 安装(99% 用户选这个)

1. 安装 Homebrew(如果还没装)

打开 终端(Terminal),粘贴运行:

1

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

安装过程会提示输入密码并按回车。

2. 安装 PostgreSQL

1

brew install postgresql

自动安装最新稳定版(如 PostgreSQL 17),并包含:

  • psql 命令行工具
  • pg_dump、createdb 等
  • pgAdmin 可选(后面教你装)

3. 启动 PostgreSQL 服务

1

2

3

4

5

6

# 启动服务(当前会话)

brew services start postgresql

 

# 或手动启动(推荐)

pg_ctl -D /opt/homebrew/var/postgres start   # Apple Silicon

# pg_ctl -D /usr/local/var/postgres start    # Intel 旧版

4. 设置开机自启(可选)

1

brew services start postgresql

以后系统启动时自动运行 PostgreSQL。

5. 验证安装成功

1

2

3

4

5

psql --version

# 输出类似:psql (PostgreSQL) 17.0

 

# 进入数据库

psql postgres

在 psql 中运行:

1

2

3

SELECT version();

\l

\q

二、其他安装方式(了解即可)

方式 说明 推荐度
Postgres.app 拖拽式 GUI 应用,一键启动 ★★★★
官方安装包 .dmg 安装,带 pgAdmin ★★★
Docker 容器化,隔离环境 ★★★★

三、Postgres.app 方式(适合新手 / 想图形化启动)

  1. 下载:https://postgresapp.com
  2. 拖到 应用程序 文件夹
  3. 双击打开 → 自动初始化并启动
  4. 菜单栏有小象图标,可一键启停

默认数据目录:~/Library/Application Support/Postgres/var-17

四、图形化管理工具(推荐安装一个)

方式 1:安装 pgAdmin 4(官方 GUI)

1

brew install --cask pgadmin4

安装后从 Launchpad 打开,添加本地服务器:

  • Host: localhost
  • Port: 5432
  • User: 你的 mac 用户名(Homebrew 版默认)

方式 2:DBeaver(免费多数据库 GUI)

1

brew install --cask dbeaver-community

五、Homebrew 版 PostgreSQL 重要路径

路径 说明
/opt/homebrew/var/postgres 数据目录(M1/M2/M3)
/usr/local/var/postgres 数据目录(Intel 旧版)
/opt/homebrew/bin/psql 命令行工具
/opt/homebrew/etc/postgresql@17 配置文件

六、设置密码 & 创建用户(推荐)

Homebrew 安装的 PostgreSQL 默认无密码,通过本地 socket 认证。

设置postgres角色密码:

1

psql postgres

1

2

ALTER USER postgres WITH PASSWORD 'your_secure_password';

\q

创建应用专用用户和数据库

1

psql postgres

1

2

3

4

CREATE DATABASE myapp;

CREATE USER appuser WITH ENCRYPTED PASSWORD 'apppass123';

GRANT ALL PRIVILEGES ON DATABASE myapp TO appuser;

\q

测试连接:

1

psql -h localhost -U appuser -d myapp

七、允许远程连接(开发/测试用)

1. 修改监听地址

1

nano /opt/homebrew/var/postgres/postgresql.conf

找到并修改:

1

2

# listen_addresses = 'localhost'

listen_addresses = '*'

2. 修改认证方式

1

nano /opt/homebrew/var/postgres/pg_hba.conf

末尾添加:

1

2

3

4

5

# 允许局域网

host    all             all             192.168.1.0/24          md5

 

# 允许所有(仅测试!)

# host    all             all             0.0.0.0/0               md5

3. 重启服务

1

brew services restart postgresql

4. 开放 macOS 防火墙(系统设置 → 网络 → 防火墙)

或命令行:

1

sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add /opt/homebrew/bin/postgres

八、常用命令总结(终端)

命令 说明
brew services start postgresql 启动服务
brew services stop postgresql 停止服务
brew services restart postgresql 重启
psql postgres 进入数据库
pg_dump mydb > backup.sql 备份
psql mydb < backup.sql 恢复
createdb mydb 创建数据库
dropdb mydb 删除数据库
pg_ctl status 查看状态

九、备份与恢复

1

2

3

4

5

6

7

8

# 备份

pg_dump -U postgres myapp > myapp_backup.sql

 

# 恢复

psql -U postgres myapp < myapp_backup.sql

# 或

createdb myapp_restore

psql -U postgres myapp_restore < myapp_backup.sql

十、卸载 PostgreSQL(彻底清理)

1

2

3

4

5

6

7

8

9

10

11

12

# 停止服务

brew services stop postgresql

 

# 卸载

brew uninstall postgresql

 

# 删除数据(谨慎!)

rm -rf /opt/homebrew/var/postgres

rm -rf /usr/local/var/postgres

 

# 删除配置文件

rm -rf /opt/homebrew/etc/postgresql@*

十一、常见问题(FAQ)

问题 解决方法
psql: error: could not connect to server brew services start postgresql
FATAL: role "postgres" does not exist 重新初始化:initdb /opt/homebrew/var/postgres
permission denied for database postgres 检查数据目录权限:chmod 700 /opt/homebrew/var/postgres
想换端口 修改 postgresql.conf 中的 port = 5433

十二、学习资源

  • 官方文档:https://www.postgresql.org/docs/
  • Postgres.app:https://postgresapp.com
  • Homebrew 公式:https://formulae.brew.sh/formula/postgresql

恭喜!你已成功在 macOS 上安装 PostgreSQL!


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 :
相关文章
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计