centos下编译安装PostgreSQL数据库的教程

  PostgreSQL是开源关系型数据库的首选了,因为现在mysql数据库都给oracle收购了,下面我们来介绍在centos中编译PostgreSQL数据库的教程.

  readline是一个开源的跨平台程序库,提供了交互式的文本编辑功能。postgresql需要readline的支持。

  wget -c https://ftp.postgresql.org/pub/source/v9.3.5/postgresql-9.3.5.tar.gz

  [root@rootop postgresql-9.3.5]# yum install readline readline-devel

  [root@rootop postgresql-9.3.5]# ./configure --prefix=/usr/local/pgsql

  [root@rootop postgresql-9.3.5]# make

  [root@rootop postgresql-9.3.5]# make install

  添加系统账户:

  [root@rootop ~]# useradd postgres

  [root@rootop ~]# passwd postgres

  创建数据目录:

  [root@rootop ~]# mkdir /usr/local/pgsql/data

  [root@rootop ~]# chown postgres:postgres /usr/local/pgsql/data/

  初始化数据库:

  [root@rootop ~]# su postgres  #切换到postgres用户执行

  [postgres@rootop ~]$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/

  根据提示可以通过 /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data/

  或 /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l logfile start 启动服务。

  推荐下面的脚本启动方式,启动以后会在tcp上监听5432端口。

  [postgres@rootop ~]$ lsof -i:5432

  COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

  postgres 5140 postgres 3u IPv4 2394876345 0t0 TCP localhost:postgres (LISTEN)

  复制管理脚本(root操作):

  [root@rootop postgresql-9.3.5]# cp contrib/start-scripts/linux /etc/init.d/postgresql

  [root@rootop postgresql-9.3.5]# chmod o+x /etc/init.d/postgresql

  编辑启动脚本,注意以下部分为实际信息:

  #安装路径

  prefix=/usr/local/pgsql

  #数据目录

  PGDATA=”/usr/local/pgsql/data”

  #启动用户

  PGUSER=postgres

  #日志路径

  PGLOG=”$PGDATA/serverlog”

  然后就可以通过service postgresql start|stop|restart|reload|status 管理了。

  开机启动:

  [root@AY131126202614070132Z ~]# chkconfig postgresql on

  相关配置文件:

  通过 /usr/local/pgsql/data/postgresql.conf 可以配置监听地址、端口及连接数等。

  listen_addresses =

  port =

  max_connections =

  通过 /usr/local/pgsql/data/pg_hba.conf 可以配置允许远程连接的地址。

  host all all 127.0.0.1/32 trust

  登陆数据库:

  [root@AY131126202614070132Z ~]# /usr/local/pgsql/bin/psql -h 127.0.0.1 -d postgres -U postgres

  psql (9.3.5)

  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)

  postgres=# \q   #退出

  psql 支持的参数可以通过/usr/local/pgsql/bin/psql --help 获取

  安装完成。