1.准备安装源
下载地址:https://www.postgresql.org/ftp/source/
下载并解压。
2.软件编译安装
配置、检查安装环境
./configure
--prefix=/opt/postgre94
--with-pgport=5432
--with-perl
--with-tcl
--with-python
--with-openssl
--with-pam
--without-ldap
--with-libxml
--with-libxslt
--enable-thread-safety
--with-wal-blocksize=16
--with-blocksize=16
--enable-dtrace
--enable-debug
成功后,方可进入下一步。遇到问题参考[configure遇到的问题]
编译安装
gmake world
gmake check-world (这个需要使用普通用户执行,可选,耗时较长)
gmake install-world
3.配置数据库
内核参数配置
# vi /etc/sysctl.conf #追加下面内容
kernel.shmmni = 4096
kernel.sem = 50100 64128000 50100 1280
fs.file-max = 7672460
net.ipv4.ip_local_port_range = 9000 65000
net.core.rmem_default = 1048576
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 104857
# sysctl -p #使配置成效
用户配置
# useradd postgres
# passwd postgres
# su - postgres
> vi .bash_profile #追加下面内容
export PGPORT=5432
export PGDATA=/pgdata/pg_root
export LANG=en_US.utf8
export PGHOME=/opt/postgre94
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH:.
export MANPATH=$PGHOME/share/man:$MANPATH
export PGUSER=postgres
export PGHOST=$PGDATA
初始化数据库
(配置一个数据库实例)
# su - postgres
> initdb -D $PGDATA -E UTF8 --locale=C -U postgres -W
-U 为指定管理员用户
-W 输入管理员密码
-D 数据库目录
配置数据库访问权限
# vi $PGDATA/pg_hba.conf
host all all 192.168.25.0/24 md5
# vi $PGDATA/postgresql.conf
listen_addresses = '*'
如果要在外网访问数据库,需要关闭防火墙或者配置iptables,参见Linux>配置iptables
4.基本操作
起停服务
> pg_ctl start/stop -D PGDATA
查看运行情况
#监听端口情况
> netstat -auntp | grep 5432
#进程情况
> ps -ef | grep postgres
连接数据库
> psql -h localhost postgres
postgres=# \s #查看数据库
5.同一主机多个postgresql数据库
之前的第2步编译和安装,安装的是postgresql这个软件,里面主要是postgresql软件的可执行文件,比如:初始化数据库、恢复数据库和psql等。
如果需要在同一主机上同时跑多个数据库实例,只需要再配置用户,初始化数据库及配置就可,注意的是每个实例监听的端口应该都不一样。