时间:2019-12-02来源:系统城作者:电脑系统城
容我开头啰嗦一下。一直以来,我和 MySQL 这位久经考验的老朋友合作愉快。但自从了解了一点 PostgreSQL 后, 对其丰富的功能特性就十分着迷。比如字段类型原生支持 json, xml 和 array。跟 MySQL 比起来,感觉 PostgreSQL 更高级一些。
安装brew
官方文档:
http://mxcl.github.com/homebrew/ 
先安装Git,打开一个shell:
| 1 2 3 4 5 6 7  | 
			cd /usr/localsudo mkdir homebrewcurl -L https://github.com/mxcl/homebrew/tarball/master | sudo tar xz --strip 1 -C homebrewcd homebrew/bin./brew -vfile brewcat brew | moresudo ./brew update | 
		
| 1 | sudo chown $USER /usr/localbrew updat | 
		
在".bash_profile"中更新路径配置
(如果~下没有文件".bash_profile" 请执行: touch '.bash_profile' )
| 1 | vim '.bash_profile' | 
		
| 1 | export PATH=$PATH:/usr/local/homebrew/bin | 
		
之后可以直接执行brew(不用./brew)
如果有了Git可以这样安装(未测试)
| 1 2 3 4  | 
			git clone https://github.com/mxcl/homebrew.gitcd homebrew/bincd homebrew/bin./brew -v | 
		
| 1 2 3  | 
			./brew install wget./brew uninstall wget./brew searc /apache*/ | 
		
安装PostgreSQL
使用 brew 一句搞定。
| 1 | brew install postgres | 
		
| 1 | initdb /usr/local/var/postgres | 
		
启动或停止数据库
启动服务:
| 1 | pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start | 
		
| 1 | pg_ctl -D /usr/local/var/postgres stop -s -m fast | 
		
如果先嫌麻烦,可以加入开机自动启动
| 1 | ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents | 
		
1.创建一个 PostgreSQL 用户
| 1 2 3  | 
			createuser username -P#Enter password for new role:#Enter it again: | 
		
2.创建数据库
| 1 | createdb dbname -O username -E UTF8 -e | 
		
更多数据库创建信息可以 "createdb --help" 查看。
3.连接数据库
| 1 | psql -U username -d dbname -h 127.0.0.1 | 
		
还在学习中,大致觉得跟 MySQL 区别不大,都是执行标准的 SQL 语句。目前就发现 limit 语句跟 MySQL 有区别。比如 MySQL 中取前 10 条记录:
| 1 | select * from table limit 0, 10 | 
		
| 1 | select * from table limit 10 offset 0 | 
		
这样一看,倒觉得 PostgreSQL 的看起来更清晰一些。MySQL 中的 offset 是可选项,熟悉了以后用起来无妨,刚开始学时就很容易会分不清 offset 和 limit 值的位置。
2022-09-11
Windows 系统 PostgreSQL 手工安装配置方法教程图解2022-02-25
系统城教小白如何在Centos8-stream安装PostgreSQL132021-04-22
自定义函数实现单词排序并运用于PostgreSQL(实现代码)