Mac 安装 postgreSQL

postgreSQL

发音
/ˈpoʊstɡrɛs ˌkjuː ˈɛl/

真人发音版

安装

Mac 上安装 postgreSQL

1
brew install postgresql

postgreSQL 安装地址

1
where postgres

可以看到:/usr/local/bin/postgres

初始化 datebase 服务(先去建立文件夹)

1
initdb /Users/fate/fate/data/postgres

启动/关闭 数据库服务

1
2
3
4
5
6
pg_ctl -D /Users/fate/fate/data/postgres -l logfile start

pg_ctl -D /Users/fate/fate/data/postgres -l logfile stop

// 查看服务状态
pg_ctl -D /Users/fate/fate/data/postgres -l logfile status

创建数据库

数据库名不写,默认电脑用户名

1
createdb [数据库名]

登录 postgresSQL 控制台

1
2
3
psql

pqsl -U [user] -d [database] -h [host] -p [port]

-U 指定用户, -d 指定数据库, -h 指定服务器,-p 指定端口, 默认端口是 5432。

\d 查看数据库所有表格。 \d [table_name] 查看某张表格。

\l 命令列所有数据库。\q 退出查看。

  1. 创建 fate 用户
1
create user fate with password 'fate123';
  1. 删除 fate 数据库
1
drop database fate;
  1. 创建属于 fate 用户的 fate 数据库
1
create database fate owner fate;
  1. 将数据库的所有权限赋予给 fate 用户
1
grant all privileges on database fate to fate;
  1. 给 fate 用户添加创建数据库的属性
1
alter role fate createdb;
  1. 使用 fate 用户登录 fate 数据库
1
psql -U fate -d fate

卸载 postgreSQL

1
brew uninstall postgreSQL

pgAdmin 图形管理数据库

官网上下载安装包。

参考

Mac 上简单安装和使用 postgres