首先确保你的mac上已经装有homebrew,可以通过命令行查询brew版本
brew info #查看homebrew的相关信息
brew help #查看homebrew相关命令

使用homebrew安装psql(postgresql,一下简称psql)
brew install postgresql

安装过程
查看psql版本
pg_ctl -V
psql版本
初始化psql数据库
initdb /usr/local/var/postgres
可能会出现错误

初始化出现错误
解决办法,如提示解决即可
#remove or empty the directory "/usr/local/var/postgres"
#run initdb with an argument other than "...."
cd /usr/local/var/postgres
rm -r * #清空当前文件夹下的所有文件
#再次运行就可
initdb /usr/local/var/postgres
初始化成功状态


启动数据库
#方法一
brew services start postgresql
#方法二
pg_ctl -D /usr/local/var/postgres start
#方法三啊
postgres -D /usr/local/var/postgres
#方法四
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

设置启动

查看数据库的状态

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log status

创建用户和数据库
#创建用户
createuser  -P test
#创建数据库
createdb -Otest -Eutf8 test_db
#使用数据库
psql test_db

登陆数据库
psql -U test test_db -h localhost -W

卸载数据库
brew uninstall postgres
#如果设置了开机启动,更新或者删除配置
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
rm -rf ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐