PostgreSQL学习笔记(一)—— macOS下安装

  • 安装命令:brew install postgresql

  • 我的终端是zsh,所以添加环境变量到~/.zshrc

    • vim ~/.zshrc

    • export PATH=$PATH:/usr/local/Cellar/postgresql/11.4/bin

    • source ~/.zshrc

  • 之后运行psql一直报错如下:

psql: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
// 查看错误日志:cat /usr/local/var/postgres/server.log
  • 百度以及Stack Overflow上试了很多方法,最终依靠重装解决了问题,重装步骤如下:

    • brew uninstall postgresql

    • rm -rf /usr/local/var/postgres

    • gem uninstall pg

    • brew install postgres

  • 启动数据库: brew services start postgresql

  • 之后的一些基本操作

    • 创建数据库:createdb testDB

    • 进入数据库:psql testDB

    • 创建用户:create user 用户名 with password '密码';

    • 修改密码:alter user 用户名 with password '密码';

    • 查看用户列表: du

    • 查看数据库列表:l

    • 切换数据库:c 数据库名

原文地址:https://www.cnblogs.com/GaiHeiluKamei/p/11238103.html