1. postgresql tar.gz 下载安装
下载:
// 下载地址
http://www.enterprisedb.com/products/pgbindownload.do
上面地址需要选择"Advanced users can also download a zip archive of the binaries, without the installer."这一句提供的 "**zip archive**"提供的链接地址下载.
其他的方式就不要选择了.不能充分实现自定义化安装.
2. 安装
// 解压
$ tar -zxvf 安装包.tar.gz
// 添加环境变量
vim ~/.bashrc
# Pg_SQL
export PATH=/Users/HangKO/DevOps/pgsql/bin:$PATH
# 设置PG共享库的路径
export LD_LIBRARY_PATH=/Users/HangKO/DevOps/pgsql/lib:$LD_LIBRARY_PATH
# 设定数据库中数据目录环境变量
export PGDATA=/Users/HangKO/DBdata/pgdata
让环境变量配置生效
$ souce .bash_profile
执行命令,创建数据库簇:
$ initdb
显示一大串 OK 并告诉启动 pgsql服务的方式:
fixing permissions on existing directory /home/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /home/django/software/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /home/django/software/pgsql/data -l logfile start
解释下最后一句:
// -D 是数据目录, -l 是日志文件.
pg_ctl -D /home/django/software/pgsql/data -l logfile start
3. 启动和停止数据库服务
这里声明一下:
数据库服务和数据库,两者是有差别的
启动数据库服务:
pg_ctl start -D $PGDATA
或
pg_ctl start
停止数据库服务:
pg_ctl stop -D $PGDATA
或
pg_ctl stop
当然了,关闭的时候有 -m 选项,三种停止方法,这里就少做阐述吧.
4. 进入数据库
只有开启了数据库服务,才能进入数据库
// 因为本机用户名为HangKO,所以在运行了上面的命令后会有对应的用户.
// 还有一个默认的用户和数据库,用户名和数据库名都为: postgres,密码也是这个.
// 一般还是 psql -U 当前系统用户名 -d postgres
// Linux 通过输入: id 可以看到自己当前用户名
$ psql -U HangKO -d postgres
// 查看当前有什么数据库:
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+--------+----------+---------+-------+-------------------
postgres | HangKO | UTF8 | C | UTF-8 |
template0 | HangKO | UTF8 | C | UTF-8 | =c/HangKO +
| | | | | HangKO=CTc/HangKO
template1 | HangKO | UTF8 | C | UTF-8 | =c/HangKO +
| | | | | HangKO=CTc/HangKO
(3 rows)
退出 pg数据库:
postgres=# \q
5. 后话
其实还有一下基本配置,比如端口,日志什么的,新手其实不怎么需要做到.暂且就不写了.
**我上传了一份 pg9.6的pdf学习文档,需要的可以去下载,免费**
http://download.csdn.net/detail/kohang/9842191