PostgreSQL編譯安裝

獲取源碼

python

編譯安裝

對於性能型的軟件,咱們採用編譯的方式進行安裝。sql

安裝依賴

yum install -y systemtap-sdt-devel perl-ExtUtils-Embed \
pam-devel libxml2-devel libxslt-devel python-devel

編譯

./configure --prefix=/opt/pgsql-9.3.2 \
--with-perl \
--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
gmake world # 安裝了包含文檔,全部的contrib
gmake check-world -- (須要普通用戶執行。可選,耗時較長)
gmake install-world

啓動服務

軟件安裝完畢,在操做系統中新建一個普通用戶,用於初始化數據庫、開啓和關閉數據庫。數據庫

useradd postgres
su - postgres
vi ~/.bash_profile
# add
export PGDATA=/pgdata/pg_root
export LANG=en_US.utf8
export PGHOME=/opt/pgsql-9.3.2
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

建立相應的目錄並修改權限:centos

mkdir -pv /pgdata/pg_root
chown -R postgres:postgres /pgdata/pg_root
su - postgres
# 初始化數據
# initdb -D $PGDATA -E UTF8 --locale=C -U postgres -W
# 會提示輸入兩次密碼

在啓動數據庫以前,須要初始化數據庫,在初始化的過程當中,會建立配置文件等。bash

修改配置文件

在啓動以前,須要修改下pg_hba.conf及postgresql.conf文件,app

  • pg_hba.conf用於配置控制訪問數據庫的來源
  • postgresql.conf是數據庫的主要配置文件

最好調整一下內核參數:ide

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_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576 
sysctl -p

修改limits.conf配置文件:post

vi /etc/security/limits.conf
* soft nofile 131072
* hard nofile 131072
* soft nproc 131072
* hard nproc 131072
* soft core unlimited
* hard core unlimited
* soft memlock 50000000
* hard memlock 50000000

啓動數據庫性能

# pg_ctl start -D $PGDATA
# 或者使用以下的方式啓動
pg_ctl -D /var/lib/pgsql/data -l logfile start
-bash-4.2$ lsof logfile
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 30772 postgres 1w REG 8,3 0 34606128 logfile
postgres 30772 postgres 2w REG 8,3 0 34606128 logfile
-bash-4.2$ lsof -i:5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 30771 postgres 3u IPv6 37671946 0t0 TCP localhost:postgres (LISTEN)
postgres 30771 postgres 4u IPv4 37671947 0t0 TCP localhost:postgres (LISTEN)

容許外網訪問:學習

echo "host all all 0.0.0.0/0 md5" >> $PGDATA/pg_hba.conf

中止

pg_ctl stop -m fast|smart|immediate -D $PGDATA

CentOS二進制包安裝

若是認爲CentOS或RedHat自帶的PostgreSQL版本過低,想要使用新的版本,能夠使用下面的方法安裝。安裝PostgreSQL官方提供的RPM包,將新版本信息加入到版本庫中:

rpm -ivh https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7-x86_64/pgdg-centos94-9.4-3.noarch.rpm

而後使用yum install命令進行安裝:

yum install -y postgresql94-server.x86_64

安裝第三方貢獻包:

yum install -y postgresql94-contrib.x86_64

新版本的PostgreSQL的數據目錄在/var/lib/pgsql/<version>/data目錄下,version表示PostgreSQL的版本,如9.4版本就安裝在/var/lib/pgsql/9.4/data目錄下。

MacOS安裝PostgreSQL

能夠下載安裝Postgres.app便可,這樣比較方便學習。

相關文章
相關標籤/搜索