技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> MySQL --> 通过PostgreSQL的源代码安装数据库

通过PostgreSQL的源代码安装数据库

浏览:1741次  出处信息

准备步骤:

操作系统-Ubuntu10.04

第一步:下载源码
到官网www.postgresql.org去下载8.4.4版本的源码,我的下载地址是ftp://ftp2.cn.postgresql.org/postgresql/source/v8.4.4/postgresql-8.4.4.tar.gz
我的保存路径为:/home/jiwan/postgresql/postgresql-tar/postgresql-8.4.4.tar.gz
你可以存放到任意想存放的地方。

第二步:解压源码
cd /home/jiwan/postgresql
tar -xvf postgresql-tar/postgresql-8.4.4.tar.gz
mv postgresql-8.4.4 postgresql-source
处理后的文件目录截图:

解释一下:
postgresql-8.4.4.tar.gz中有个叫postgresql-8.4.4的文件夹;
第二行会postgresql-8.4.4.tar中的postgresql-8.4.4文件夹解压到/home/jiwan/postgresql/目录下面;
第三行将postgresql-8.4.4文件夹改名为postgresql-source。

第三步:配置
cd /home/jiwan/postgresql/postgresql-source
sudo ./configure -prefix=/home/jiwan/postgresql/postgresql-bin
执行成功的情况下,系统会有如下输出:
checking build system type… i686-pc-linux-gnu
checking host system type… i686-pc-linux-gnu
…这里省略N行输出信息…
config.status: linking ./src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking ./src/makefiles/Makefile.linux to src/Makefile.port

很不幸,一般情况下都不会成功执行,因为configure执行的时候会检查依赖的库文件是否存在。有错误的系统输出是:
./configure -prefix=/home/jiwan/postgresql/postgresql-bin
checking build system type… i686-pc-linux-gnu
…这里省略N行输出信息…
configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure. It is possible the compiler isn’t looking in the proper directory.
Use -without-readline to disable readline support.
事实是,一个干净的Ubuntu10.04还需要安装下列库:
libreadline5-dev: 产生error(readline library not found)
zlib1g-dev: 产生error(readline library not found)
bison: 产生warning,可选,用于cvs支持等等
flex: 产生warning,可选,用于cvs支持等等
解决方法是:安装之前执行如下命令(也可以逐个安装以上四个库)
sudo apt-get install libreadline5-dev zlib1g-dev bison flex
其实这条命令还会自动安装libncurses5-dev, libreadline5和m4三个库

解释一下:
./configure是一个批处理文件,它会检查编译安装需要的各种文件并配置各个参数。
通过-prefix指定数据库系统的安装位置:后面的安装步骤会把运行数据库系统需要的文件(二进制,配置文件等等)拷贝到/home/jiwan/postgresql/postgresql-bin目录下面。
可以用./configure -help来查看可用的参数。
configure执行之后的屏幕输出有三种:
以cheking开头的输出:库依赖的结果;
以configure开头的输出:修改了配置选项
以config.status开头的输出:产生的配置文件;
如果安装了bison和flex,执行configure批处理文件之后,下一步编译的时候,就会使编译好后的数据库系统支持bison和flex。

第四步:编译并安装
cd /home/jiwan/postgresql/postgresql-source
make
make install
解释一下:
这一步的目的是:把源码编译成可执行文件和共享库文件,并复制到指定的”/home/jiwan/postgresql/postgresql-bin”目录下面。值得注意的是,这样做以后数据库系统还需要一些初始化的操作才可以使用。
make会默认调用Makefile文件,然后把源码全部编译;
make install会把PostgreSQL数据库安装到”/home/jiwan/postgresql/postgresql-bin”目录下面(这个目录是上一步中设置的),成功的标志就是:”/home/jiwan/postgresql/postgresql-bin”目录下面会有四个文件夹:
bin: 存放可执行文件
include: 存放源代码的头文件
lib: 存放共享库文件
share: 存放数据库系统的公共配置,文档等等

第五步:创建linux帐号”postgres”并设置密码为”postgres”
sudo adduser postgres

第六步:创建数据库初始化需要的文件夹并更改所有者
cd /home/jiwan/postgresql/postgresql-bin
mkdir data
sudo chown -R postgres /home/jiwan/postgresql/postgresql-bin

第七步:初始化数据库
su - postgres
cd /home/jiwan/postgresql/postgresql-bin
bin/initdb -D data
最后一行命令的屏幕输出是:
The files belonging to this database system will be owned by user “postgres”.
…这里省略N行输出信息…
Success. You can now start the database server using:
bin/postgres -D data
or
bin/pg_ctl -D data -l logfile start

解释一下:
这一步创建了数据库运行所需的数据文件和配置文件

第八步:启动数据库服务
su - postgres
cd /home/jiwan/postgresql/postgresql-bin
bin/postmaster -D data>logfile 2>&1 &
数据库成功启动服务之后,默认会在本机地址127.0.0.1的5432端口侦听连接请求,通过显示本机正在侦听的进程可以检查数据库服务是否成功运行:
su root -c “netstat -lnpt|grep post”
应该输出如下结果(postmaster是指向postgres程序的一个链接):
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 21288/postmaster
tcp6 0 0 ::1:5432 :::* LISTEN 21288/postmaster

第九步:创建数据库
su - postgres
/home/jiwan/postgresql/postgresql-bin/bin/createdb jiwan

第十步:连接数据库
su - postgres
/home/jiwan/postgresql/postgresql-bin/bin/psql
这条命令会自动以postgres命令连接到postgres数据库,连接后系统的输出为:
―――――――――――
psql (8.4.4)
Type “help” for help.
postgres=#
―――――――――――
可以在提示符”postgres=#”后面输入”\l”检查之前创建的数据库”jiwan”是否存在,存在的标志就是,执行之后屏幕输出包含以下一行:
jiwan | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |

第十一步:修改配置文件,使得其他主机可以访问数据库
需要修改的文件在”/home/jiwan/postgresql/postgresql-bin/data”目录之下,文件为:
postgresql.conf
pg_hba.conf.
配置方法参考Ubuntu下Postgresql-8.4安装及配置

建议继续学习:

  1. 也谈PostgreSQL的同步配置(Slony)    (阅读:4490)
  2. 多版本并发控制:PostgreSQL vs InnoDB    (阅读:3514)
  3. Ubuntu下Postgresql-8.4安装及配置    (阅读:3293)
  4. PostgreSQL与MySQL的区别    (阅读:2907)
  5. Ubuntu下PostgreSQL数据库集群(PL/Proxy)配置方法    (阅读:2922)
  6. PostgreSQL 9.1的新特性    (阅读:2466)
  7. PostgreSQL    (阅读:2371)
  8. PostgreSQL从菜鸟到专家 PostgreSQL介绍    (阅读:2349)
  9. PostgreSQL安装    (阅读:2220)
  10. 在Linux上编译安装PostgreSQL8.3.X    (阅读:2235)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1