在 macOS 上安装 PostgreSQL 有多种方法,这里介绍一种使用 Homebrew 安装的方案
使用 Homebrew 安装 PostgreSQL
1. 安装 Homebrew(如果尚未安装): 打开终端并运行以下命令:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
2. 安装 PostgreSQL:
安装 Homebrew 后,运行以下命令来安装 PostgreSQL:
brew install postgresql
安装完成之后,可以通过命令查看是否已经安装完成:
psql --version
psql (PostgreSQL) 14.13 (Homebrew) # 输出版本号,说明已经成功安装
3. 启动 PostgreSQL 服务:
安装完成后,你可以启动 PostgreSQL 服务:
brew services start postgresql
4. 创建数据库用户:
默认情况下,PostgreSQL 会默认创建一个名为postgres的数据库,用户名为你的 macOS 用户名,使用下面这个命令可以登陆:
psql -U $(whoami) -d postgres
5. 设置密码:
在 psql 命令行界面中,为默认用户设置密码:
\password
-
系统会提示你输入新密码并确认。
-
输入完成后,密码会立即生效。
例如:
postgres=# \password
Enter new password for user "your_username":
Enter it again:
postgres=#
PostgreSQL 默认使用 peer 认证(基于 macOS 用户名),无需密码
6. 停止 PostgreSQL 服务(可选):
如果你想停止 PostgreSQL 服务,可以运行:
brew services stop postgresql