PostgreSQL 作为一款优秀的开源关系型数据库产品,一直受到各大企业和IT人士的关注。虽然在中国的流行度远远落后于MySQL,但是相信随着开源社区的宣传,还有其优秀的特性,PostgreSQL在未来中国数据库市场肯定会大放异彩!

   PostgreSQL起源于INGRES,后经伯克利大学2名学生改进,重写了SQL解释器,96年发布了第一个正式的开源版本,此后PostgreSQL便交由社区维护。最近因为公司业务的需要我开始接触PostgreSQL的运维管理,相信如果你使用过MySQL或者Oracle,再来接触PostgreSQL,一股浓浓的学院风便扑鼻而来。

   今天,向大家介绍一下这款数据库产品的编译安装。你可以通过http://www.postgresql.org这个网站进行源码的下载,我使用的是postgresql-9.3.2这个版本。


[root@eq2-cndbavmdb-01 local]# pwd
/usr/local
[root@eq2-cndbavmdb-01 local]# ls -l postgresql-9.3.2.tar.gz
-rw-r--r-- 1 root root 22218672 Jan  1 04:22 postgresql-9.3.2.tar.gz


   解压这个tar包。。。

[root@eq2-cndbavmdb-01 local]# tar -zxf postgresql-9.3.2.tar.gz

   进入解压出来的目录postgresql-9.3.2,在这里面我们便可以看到一些子目录和文件

[root@eq2-cndbavmdb-01 postgresql-9.3.2]# ll
total 2504
-rw-r--r--  1 1107 1107     385 Dec  2 12:57 aclocal.m4
drwxrwxrwx  2 1107 1107    4096 Dec  2 13:10 config
-rwxr-xr-x  1 1107 1107  888391 Dec  2 12:57 configure
-rw-r--r--  1 1107 1107   65742 Dec  2 12:57 configure.in
drwxrwxrwx 56 1107 1107    4096 Dec  2 13:09 contrib
-rw-r--r--  1 1107 1107    1192 Dec  2 12:57 COPYRIGHT
drwxrwxrwx  3 1107 1107    4096 Dec  2 13:10 doc
-rw-r--r--  1 1107 1107    3767 Dec  2 12:57 GNUmakefile.in
-rw-r--r--  1 1107 1107 1471819 Dec  2 13:12 HISTORY
-rw-r--r--  1 1107 1107   76689 Dec  2 13:12 INSTALL
-rw-r--r--  1 1107 1107    1489 Dec  2 12:57 Makefile
-rw-r--r--  1 1107 1107    1284 Dec  2 12:57 README
drwxrwxrwx 15 1107 1107    4096 Dec  2 13:12 src

  其中,contrib这个目录里面包含了一些第三方贡献的数据库插件。INSTALL里面写了一些安装说明。目录src里面是源代码。

  在编译安装前,需要进行一些参数的配置,我们可以通过运行./configure --help查看相关可以配置的参数以及简要说明.比如说--prefix是选择安装的路径,再比如--with-blocksize是用来指定数据块大小的参数,如果我们的系统是数据量增加比较快的系统,那么这个数据库大小可以设置的大一些,比如16k.如果系统是变更比较频繁的OLTP系统,那么我们可以选择相当较小的数据块大小,比如8k。

  首先配置参数,生成makefile.(因为是测试环境,所以只选了2个参数,大家可以根据自己的需要参照官方文档或者./configure --help选择合适的参数)

[root@eq2-cndbavmdb-01 postgresql-9.3.2]# ./configure  --prefix=/usr/local/postgres  --with-blocksize=8

  接下来是进行编译。我们可以直接make,也可以make world, make world的好处就是它可以把相关的第三方插件一起进行编译。

[root@eq2-cndbavmdb-01 postgresql-9.3.2]# make world

  编译好以后就可以安装了,make install-world

[root@eq2-cndbavmdb-01 postgresql-9.3.2]# make  install-world

  安装好以后我们就会看到PostgreSQL, contrib, and documentation installation complete.这样的提示语句。

  现在我们需要建立一个普通用户来启动和管理PostgreSQL数据库。

[root@eq2-cndbavmdb-01 ~]# useradd -d /usr/local/postgres postgres
[root@eq2-cndbavmdb-01 local]# chown -R postgres:dba postgres

 修改这个用户的.bash_profile 添加对应的环境变量


export PGHOME=/usr/local/pgsql
export PGDATA=/usr/local/pgsql/data
export PATH=$PATH:/usr/local/pgsql/bin
export PGPORT=1921

 初始化数据库 -D 是数据库数据存放目录,-E是指定编码 -U 是指定一个超级用户, -W是为这个用户设置密码。

[postgres@eq2-cndbavmdb-01 ~]$ initdb -D $PGDATA -E UTF8 -U postgres -W
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /usr/local/postgres/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
creating configuration files ... ok
creating template1 database in /usr/local/postgres/data/base/1 ... ok
initializing pg_authid ... ok
Enter new superuser password:
Enter it again:
setting password ... 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:
    postgres -D /usr/local/postgres/data
or
    pg_ctl -D /usr/local/postgres/data -l logfile start

在启动数据库之前,我们首先来看一下数据库中的重要配置文件。

在$PGDATA目录里面我们可以找到pg_hba.conf这个文件,这个文件主要是用来控制数据库的连接访问。我添加了host all all 0.0.0.0/0 md5 就表示我允许任何IP的任何用户通过md5这个验证方法来访问我的所有数据库,当然产品库上面大家最好做一个限制。

# TYPE  DATABASE        USER            ADDRESS                 METHOD
local   all             all                                     trust
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 trust
host    all             all             0.0.0.0/0               md5

然后就是最重要的配置文件postgresql.conf

因为是测试环境,我修改和打开了几个参数,大家可以根据自己的需求进行更改和配置

listen_addresses = '*'
port = 1921
max_connections = 100
superuser_reserved_connections = 10
unix_socket_directories = '.'
unix_socket_permissions = 0700
shared_buffers = 256MB
temp_buffers = 8MB
wal_level = minimal
fsync = on
synchronous_commit = on
wal_sync_method = fsync
checkpoint_segments = 16
log_destination = 'stderr'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_file_mode = 0600
log_rotation_size = 10MB
log_checkpoints = on
log_connections = on
log_disconnections = on
log_error_verbosity = verbose
log_line_prefix = '%t'

启动数据库 :

[postgres@eq2-cndbavmdb-01 data]$ pg_ctl start
server starting
[postgres@eq2-cndbavmdb-01 data]$ 2014-01-16 03:29:58 PSTLOG:  00000: redirecting log output to logging collector process
2014-01-16 03:29:58 PSTHINT:  Future log output will appear in directory "pg_log".
2014-01-16 03:29:58 PSTLOCATION:  SysLogger_Start, syslogger.c:649

使用psql连接到数据库:

[postgres@eq2-cndbavmdb-01 data]$ psql -h 127.0.0.1 -p 1921 -U postgres
psql (9.3.2)
Type "help" for help.
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

到此数据库安装完毕!