在linux下的安装postgresql

2009年07月29日 作者: 大头刚 

1.下载并解压
官方网站下载安装文件,可以选择编译好的,也可选择源代码安装,本例使用源代码
最新版本是8.3.7(目前到了8.4),下载到安装目录,解压
cd /usr/local/
tar -xzvf postgresql-8.3.7.tar.gz

2.添加postgresql用户

groupadd postgresql
useradd postgresql -g postgresql
mkdir pgsql
chown -R postgresql.postgresql pgsql/
cd postgresql-8.3.7

3.开始编译安装

./configure --prefix=/usr/local/pgsql 
make
make install
 
如果遇到下面报错
configure: error: readline library not found
方法1: 安装该软件包
apt-get install libreadline5 libreadline5-dev
方法2: --without-readline 选项关闭 readline 功能,即:
./configure --prefix=/usr/local/pgsql --without-readline

4.添加环境变量

su - postgresql
cat .bash_profile 
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATHHOME/bin
export PATH
unset USERNAME
export PATH="$PATH":/usr/local/pgsql/bin 
export POSTGRES_HOME=/usr/local/pgsql 
export PGLIB=$POSTGRES_HOME/lib 
export PGDATA=$POSTGRES_HOME/data 
export MANPATH="$MANPTH"POSTGRES_HOME/man 
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB"

5.再初始话数据库

/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data 
 
Success. You can now start the database server using:
    /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
or
    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start

6.启动数据库

pg_ctl start
server starting
postgresql@www:/usr/local/pgsql/data$ LOG:  database system was shut down at 2009-06-17 17:15:14 CST
LOG:  autovacuum launcher started
LOG:  database system is ready to accept connections
 
OK,进入数据
psql template1
Welcome to psql 8.3.7, the PostgreSQL interactive terminal.
 
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit
template1=# select version();
                                         version                                          
------------------------------------------------------------------------------------------
PostgreSQL 8.3.7 on i686-pc-linux-gnu, compiled by GCC gcc (Ubuntu 4.3.3-5ubuntu4) 4.3.3

PostgreSQL很多方面和Oracle比较相似,安装和Mysql一样比较简单。不过在国内应用好像不多,相关资料很少。有兴趣的朋友不妨一起交流。