PostgreSQL16安装Mac(brew)

问题

最近需要从MySQL切换到PostgreSQL。我得在本地准备一个PostgreSQL。

步骤

使用brew安装postgresql16:

bash 复制代码
arch -arm64 brew install postgresql@16

启动postgresql16:

bash 复制代码
brew services start postgresql@16

配置postgresql环境变量,打开环境变量文件:

bash 复制代码
open ~/.zshrc

配置如下内容:

bash 复制代码
export PG_HOME=/opt/homebrew/opt/postgresql@16
export PATH=$PG_HOME/bin:$PATH

重载环境变量:

bash 复制代码
source ~/.zshrc

查看postgresql版本:

bash 复制代码
psql --version

查询数据库管理员用户名:

bash 复制代码
id -un

登录数据库:

bash 复制代码
psql -U <管理用户名> postgres

修改管理用户密码:

sql 复制代码
ALTER USER <管理用户名> PASSWORD '<密码>';

使用\q退出psql客户端。查看登录设置配置文件:

bash 复制代码
sudo -u <管理员用户名> psql postgres -c "SHOW hba_file;"

结果:

bash 复制代码
                  hba_file
---------------------------------------------
 /opt/homebrew/var/postgresql@16/pg_hba.conf
(1 行记录)

打开pg_hba.conf文件,设置需要使用密码方式登录postgresql,将trust改成scram-sha-256,如下图:

注意,这里的trust是不需要验证用户就可以直接使用;passwords是明文传用户密码给postgresql服务器;md5scram-sha-256密码加密给postgresql服务器进行登录。

重启服务:

bash 复制代码
brew services restart postgresql@16

再次验证使用登录:

bash 复制代码
psql -U <管理员用户> postgres

总结

postgresql还是很简单的,只要需要注意postgresql默认表名和字段名是小写的,默认也不支持反引号(`)。需要注意下与mysql区别的这些注意点。还有就是写sql使用函数的时候,尽可能使用大家这几个数据库都支持的sql函数。

参考

相关推荐
l1t1 小时前
DeepSeek 4.1总结的Tom Lane 谈塑造 Postgres 三十年历程的架构决策
开发语言·数据库·postgresql·架构
PrudentWoo15 小时前
PostgreSQL 12 登录失败:role “postgres“ is not permitted to log in 的排查与修复
数据库·postgresql
Meta3916 小时前
PostgreSQL报SELECT rule‘s target entry X has different type from column “XXX“
数据库·postgresql·dubbo
钱栈up17 小时前
Mac端一键部署服务端项目:从流程梳理到踩坑适配的完整实践
macos
天天喝旺仔19 小时前
PostgreSQL 慢查询治理实战:读懂 EXPLAIN ANALYZE 执行计划,落地复合索引、部分索引与覆盖索引
sql·postgresql·性能优化·数据库开发
2601_9622838821 小时前
Django数据库配置(一)
mysql·postgresql·django·sqlite·数据库配置
AugustSkys1 天前
PostgreSQL 完全指南:特性对比 + 安装部署 + 配置管理 + psql 实战 + pgAdmin 图形化
运维·postgresql·开源数据库·pgadmin
for_ever_love__1 天前
iOS: GCD进阶,全局队列与自定义队列
macos·ios·objective-c·cocoa·多线程·gcd
蓝丶曦月1 天前
Notion MacBook Apple M4 Pro 安装更新
macos·notion
必须会一定会1 天前
Spring Boot 3 + PostgreSQL 场景工程持久化:revision、contentHash 与历史回滚
人工智能·spring boot·后端·postgresql·ai编程