debian下安装PostgreSQL

下载源码

解压缩

然后安装库

apt-get install auto-apt automake autoconf

apt-get install libreadline-dev

 

build-essential 也要

 (另外一个教程:aptitude install build-essential zlib1g-dev libpam0g-dev libssl-dev libperl-dev kernel-package libncurses5-dev flex bison gawk chkconfig)

搞不清推荐都装好

 

 

 

设置环境变量
  vi /etc/profile
  export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH
  export PATH=/usr/local/pgsql/bin:$PATH
  export PGDATA=/usr/local/pgsql/data
  export MANPATH=$MANPATH:/usr/local/pgsql/man

 

 

CD到源码根目录

先执行  ./configure

 

如果报错的话,优已经装好必要软件,则可以试试忽略错误的方式

 ./configure --without-readline --without-zlib

 

 

然后 make

最好 make install

/usr/local/pgsql 这个是默认的安装目录

建立postgresql数据库专用普通权限用户 :adduser psqlroot

建立数据库文件夹 如: home/data

mkdir /home/data

权限 : chown psqlroot:psqlroot date

 

切花数据库用户 su psqlroot

cd  到 pgslq的安装目录 bin文件夹下 执行 以下命令  


root@www18130ui:/# cd /usr/local
root@www18130ui:/usr/local# cd pgsql
root@www18130ui:/usr/local/pgsql# cd bin
root@www18130ui:/usr/local/pgsql/bin# su psqlroot
psqlroot@www18130ui:/usr/local/pgsql/bin$ ./initdb -D /home/data

初始化数据库  ./initdb -D /home/data (只能用 psqroot)

 

到数据库目录 修改 pg_hba.conf 里面加入要登录的IP

修改postgresql.conf中的listen_address=* 及端口  去掉注释#

启动服务。。。测试下

 

postgres -D /usr/local/pgsql/data

or
pg_ctl -D /usr/local/pgsql/data -l logfile start

远程就可以正常登录了

 

 

第二阶段-制作启动脚本:
1,从源代码目录里面复制出原始的开机脚本:
cp /usr/src/postgresql-9.1.3/contrib/start-scripts/linux    /etc/init.d/postgresql
2,给予执行权限:chmod 700 /etc/init.d/postgresql
3,建立s 2 3 4 5 6各个级别启动连接在(/etc/rcS.d rc1.d rc2.d rc3.d rc4.d rc5.d rc6.d)
   使用:chkconfig /etc/init.d/postgresql自动建立 呵呵很方便哦!
4,修改/etc/init.d/postgresql文件,这个文件是用/bin/sh基础的,改成/bin/bash的:

#! /bin/bash

# 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 <pgsql@rkirkpat.net>

# contrib/start-scripts/linux

## EDIT FROM HERE

# Installation PREFIX
PREFIX=/usr/pgsql-9.1.3

# Data directory
PGDATA=$PREFIX/date

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

# Where to keep a log file
PGLOG=/var/log/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_ADJ value to
# -17 will disable OOM kill altogether.  If you enable this, you probably want
# to compile PostgreSQL with "-DLINUX_OOM_ADJ=0", so that individual backends
# can still be killed by the OOM killer.
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_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"
 echo "ok"
 ;;
  restart)
 echo -n "Restarting PostgreSQL: "
 su - $PGUSER -c "$PGCTL stop -D '$PGDATA' -s -m fast -w"
 test x"$OOM_ADJ" != x && echo "$OOM_ADJ" > /proc/self/oom_adj
 su - $PGUSER -c "$DAEMON -D '$PGDATA' &" >>$PGLOG 2>&1
 echo "ok"
 ;;
  reload)
        echo -n "Reload PostgreSQL: "
        su - $PGUSER -c "$PGCTL reload -D '$PGDATA' -s"
        echo "ok"
        ;;
  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

5,注意上文提到的OOM_ADJ=-17 是为了逃避OOM自动封杀,OK!执行/etc/init.d/postgresql start stop restart 等等命令试试,大工告成!!
6,把安装目录中的conf配置文件复制到/etc/postgresql下面去,方便以后更改配置
  mkdir /etc/postgresql
  mv /usr/pgsql-9.1.3/date/*conf /etc/postgresql/
  cd /usr/pgsql-9.1.3/date
  su - psqlroot -c 'ln -s /etc/postgresql/pg_hba.conf pg_hba.conf'
  su - psqlroot -c 'ln -s /etc/postgresql/pg_ident.conf pg_ident.conf'
  su - psqlroot -c 'ln -s /etc/postgresql/postgresql.conf postgresql.conf'

posted on 2013-04-03 13:27  neights  阅读(921)  评论(0编辑  收藏  举报

导航