天天看點

centos6.5下postgresql9.4.3安裝與配置

一、簡介

PostgreSQL 是一種非常複雜的對象-關系型資料庫管理系統(ORDBMS),也是目前功能最強大,特性最豐富和最複雜的自由軟體資料庫系統。有些特性甚至連商業資料庫都不具備。這個起源于伯克利(BSD)的資料庫研究計劃目前已經衍生成一項國際開發項目,并且有非常廣泛的使用者。

二、系統環境

系統平台:CentOS release 6.5 (Final)

PostgreSQL 版本:PostgreSQL 9.4.3

防火牆已關閉/iptables: Firewall is not running.

SELINUX=disabled

三、源碼安裝

1. 下載下傳PostgreSQL 源碼包

# wget http://ftp.postgresql.org/pub/source/v9.4.3/postgresql-9.4.3.tar.bz2

2. 解壓源碼包

# tar xjf postgresql-9.4.3.tar.bz2

3. 進入解壓後的目錄

# cd postgresql-9.4.3

4.安裝依賴包

yum -y install gcc*

yum -y install readline-devel

5.增加使用者設定密碼

[root@postgresql ~]# adduser postgres

[root@postgresql ~]# passwd postgres

Changing password for user postgres.

New password:

BAD PASSWORD: it is too simplistic/systematic

BAD PASSWORD: is too simple

Retype new password:

passwd: all authentication tokens updated successfully.

6.開始編譯安裝PostgreSQL 資料庫。

 [root@postgresql ~]# cd postgresql-9.4.3

[root@postgresql postgresql-9.4.3]# ./configure --prefix=/home/postgres/pgsql

[root@postgresql postgresql-9.4.3]# gmake

[root@postgresql postgresql-9.4.3]# gmake install

7.設定環境變量

[root@postgresql ~]# cd /home/postgres/

[root@postgresql postgres]# ls

pgsql

[root@postgresql postgres]# vi .bash_profile

把 PATH=$PATH:$HOME/bin

改成 PATH=$PATH:$HOME/bin:/home/postgres/pgsql/bin

儲存退出。

讓環境變量生效:

在設定

[root@postgresql ~]# vi .bash_profile

[root@postgresql ~]# source .bash_profile

8.初始化資料庫

8.1建立資料目錄

[root@postgresql ~]# mkdir /home/postgres/pgsql/data

8.2更改權限

[root@postgresql ~]# chown postgres:postgres /home/postgres/pgsql/data

8.3切換到postgres使用者

[root@postgresql ~]# su - postgres

8.4 init db

[postgres@postgresql ~]$ /home/postgres/pgsql/bin/initdb -D /home/postgres/pgsql/data

到這裡資料的初始化就完成

9.系統服務

9.1回到root使用者

[postgres@postgresql ~]$ exit

9.2複制安裝目錄下的linux檔案到/etc/init.d/

進入postgresql 的安裝目錄(即剛剛使用tar指令解壓的目錄)

[root@postgresql ~]# cd postgresql-9.4.3

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

9.3修改/etc/init.d/postgresql  注意:紅色是修改部分

[root@postgresql postgresql-9.4.3]# vi /etc/init.d/postgresql

#! /bin/sh

# chkconfig: 2345 98 02

# description: PostgreSQL RDBMS

# This is an example of a start/stop script for SysV-style init, such

# as is used on Linux systems.  You should edit some of the variables

# and maybe the 'echo' commands.

#

# Place this file at /etc/init.d/postgresql (or

# /etc/rc.d/init.d/postgresql) and make symlinks to

#   /etc/rc.d/rc0.d/K02postgresql

#   /etc/rc.d/rc1.d/K02postgresql

#   /etc/rc.d/rc2.d/K02postgresql

#   /etc/rc.d/rc3.d/S98postgresql

#   /etc/rc.d/rc4.d/S98postgresql

#   /etc/rc.d/rc5.d/S98postgresql

# Or, if you have chkconfig, simply:

# chkconfig --add postgresql

# Proper init scripts on Linux systems normally require setting lock

# and pid files under /var/run as well as reacting to network

# settings, so you should treat this with care.

# Original author:  Ryan Kirkpatrick <[email protected]>

# contrib/start-scripts/linux

## EDIT FROM HERE

# Installation prefix

prefix=/home/postgres /pgsql

# Data directory

PGDATA="/home/postgres/pgsql/data"

# Who to run the postmaster as, usually "postgres".  (NOT "root")

PGUSER=postgres

# Where to keep a log file

PGLOG="$PGDATA/serverlog"

# It's often a good idea to protect the postmaster from being killed by the

# OOM killer (which will tend to preferentially kill the postmaster because

# of the way it accounts for shared memory).  Setting the OOM_SCORE_ADJ value

# to -1000 will disable OOM kill altogether.  If you enable this, you probably

# want to compile PostgreSQL with "-DLINUX_OOM_SCORE_ADJ=0", so that

# individual backends can still be killed by the OOM killer.

#OOM_SCORE_ADJ=-1000

# Older Linux kernels may not have /proc/self/oom_score_adj, but instead

# /proc/self/oom_adj, which works similarly except the disable value is -17.

# For such a system, enable this and compile with "-DLINUX_OOM_ADJ=0".

#OOM_ADJ=-17

## STOP EDITING HERE

# The path that is to be used for the script

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# What to use to start up the postmaster.  (If you want the script to wait

# until the server has started, you could use "pg_ctl start -w" here.

# But without -w, pg_ctl adds no value.)

DAEMON="$prefix/bin/postmaster"

# What to use to shut down the postmaster

PGCTL="$prefix/bin/pg_ctl"

set -e

# Only start if we can find the postmaster.

test -x $DAEMON ||

{

        echo "$DAEMON not found"

        if [ "$1" = "stop" ]

        then exit 0

        else exit 5

        fi

}

# Parse command line parameters.

case $1 in

  start)

        echo -n "Starting PostgreSQL: "

        test x"$OOM_SCORE_ADJ" != x && echo "$OOM_SCORE_ADJ" > /proc/self/oom_score_adj

        test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj

        su - $PGUSER -c "$DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1

        echo "ok"

        ;;

  stop)

        echo -n "Stopping PostgreSQL: "

        su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast"

  restart)

        echo -n "Restarting PostgreSQL: "

        su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast -w"

        test x"$OOM_SCORE_ADJ" != x && echo "$OOM_SCORE_ADJ" > /proc/self/oom_score_adj

  reload)

        echo -n "Reload PostgreSQL: "

        su - $PGUSER -c "$PGCTL reload -D '$PGDATA' -s"

status)

        su - $PGUSER -c "$PGCTL status -D '$PGDATA'"

  *)

        # Print help

        echo "Usage: $0 {start|stop|restart|reload|status}" 1>&2

        exit 1

esac

exit 0

9.4添加執行權限

[root@postgresql postgresql-9.4.3]# chmod +x /etc/init.d/postgresql

9.5啟動資料庫

[root@postgresql postgresql-9.4.3]# /etc/init.d/postgresql start

Starting PostgreSQL: ok

9.6讓資料庫開機啟動

[root@postgresql postgresql-9.4.3]# chkconfig --add postgresql

[root@postgresql postgresql-9.4.3]# chkconfig postgresql on

9.7建立資料庫操作曆史記錄檔案

[root@postgresql postgresql-9.4.3]# touch /home/postgres/pgsql/.pgsql_history

[root@postgresql postgresql-9.4.3]# chown postgres:postgres /home/postgres/pgsql/.pgsql_history

10.測試使用

[root@postgresql postgresql-9.4.3]# su - postgres

[postgres@postgresql ~]$ createdb test

[postgres@postgresql ~]$ psql test

psql (9.4.3)

Type "help" for help.

test=#

源碼編譯安裝成功。

 本文轉自 jxzhfei  51CTO部落格,原文連結:http://blog.51cto.com/jxzhfei/1677802

版權聲明:原創作品,如需轉載,請注明出處。否則将追究法律責任