在mac利用 homebrew直接安装 PostgreSQL:
brew install postgresql -v
稍等片刻,PostgreSQL 就安装完成。接下来就是初始数据库,在终端执行一下命令,初始配置 PostgreSQL:
initdb /usr/local/var/postgres -E utf8
指定 "/usr/local/var/postgres" 为 PostgreSQL 的配置数据存放目录,并且设置数据库数据编码是 utf8,更多配置信息可以 "initdb --help" 查看。
/usr/local/var/postgres
如果需要,可以设成开机启动 PostgreSQL:
ln -sfv /usr/local/opt/postgresql/\*.plist ~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
常用命令
1、启动 PostgreSQL:
pg\_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
2、关闭 PostgreSQL:
pg\_ctl -D /usr/local/var/postgres stop -s -m fast
创建一个 PostgreSQL 用户
createuser testname -P #Enter password for new role: #Enter it again:
上面的 testname 是用户名
3、创建数据库
createdb testdb -O testname -E UTF8 -e
创建testdb数据库,并指定testname为这个数据库的owner,数据库的编码(encoding)是 UTF8,参数 "-e" 是指把数据库执行操作的命令显示出来。
4、连接数据库
psql -U username -d dbname -h 127.0.0.1
原文链接:https://codingdict.com/