postgresql 安装

在Linux上安装PostgreSQL

时间:2010-11-19 10:40 来源:51CTO 收藏 复制分享 共有评论(0)条 php

 MySQL 是一条轻快的小海豚,可是缺乏不少现代关系数据库应有的特点,例如:引用完整性,视图,触发器等。所以,若是你须要开发一个电子商务的网站,须要这些功能 的话,你或许应该考虑 PostgreSQL 了。本文将经过其在 Red Hat 7.1 上安装过程,简要介绍其用法。 sql

  PostgreSQL 的官方下载地址为: 数据库

  ftp://ftp.postgresql.org/pub/v7.1.3/postgresql-7.1.3.tar.gz 安全

  http://www.postgresql.org/ bash

  若是下载最新的开发版本,你须要下载并安装 flex(版本号大于 2.5.4) 以及 bison (版本号大于 1.28) 工具

  设计人员为了安全考虑,PostgreSQL 不能以 root 用户运行,因此必须创建对应的用户和组。 post

  # useradd postgre (自动创建 postgre 组) flex

  安装的过程并不复杂和其余源码版本的安装方法相似: 网站

  解压到 /usr/local/src: spa

  # tar xvfz postgresql-7.1.3.tar.gz

  # cd postgresql-7.1.3

  # ./configure --prefix=/usr/local/pgsql

  # make

  # make install

  # chown -R postgre.postgre /usr/local/pgsql

  这样安装完毕后,并非万事大吉了,还有一些收尾工做要作:

  # vi ~postgre/.bash_profile

  添加:

  PGLIB=/usr/local/pgsql/lib

  PGDATA=$HOME/data

  PATH=$PATH:/usr/local/pgsql/bin

  MANPATH=$MANPATH:/usr/local/pgsql/man

  export PGLIB PGDATA PATH MANPATH

  以 postgres 用户登陆,

  # su - postgre

  创建数据库目录:

  $ mkdir data

  启动数据库引擎:

  $ initdb

  [postgre@www postgre]$ initdb

  This database system will be initialized with username "postgre".

  This user will own all the data files and must also own the server process.

  Fixing permissions on pre-existing data directory /home/postgre/data

  Creating database system directory /home/postgre/data/base

  Creating database XLOG directory /home/postgre/data/pg_xlog

  Creating template database in /home/postgre/data/base/template1

  Creating global relations in /home/postgre/data/base

  Adding template1 database to pg_database

  Creating view pg_user.

  Creating view pg_rules.

  Creating view pg_views.

  Creating view pg_tables.

  Creating view pg_indexes.

  Loading pg_description.

  Vacuuming database.

  Success. You can now start the database server using:

  /usr/local/pgsql/bin/postmaster -D /home/postgre/data

  or

  /usr/local/pgsql/bin/pg_ctl -D /home/postgre/data start

  $ postmaster -i -D ~/data &

  [1] 22603

  [postgre@www postgre]$ DEBUG: Data Base System is starting up at Thu Jan 31 02:00:44 2002

  DEBUG: Data Base System was shut down at Thu Jan 31 01:57:58 2002

  DEBUG: Data Base System is in production state at Thu Jan 31 02:00:44 2002

  这样 PostgreSQL 使用位于 /usr/local/pgsql/data 的数据库,容许 Internet 用户的链接( -i ) ,并在后台运行。

  创建数据库

  $createdb mydb

  PostgreSQL 会返回 “ CREATED DATABASE”的信息,代表数据库创建完成。

  $psql mydb

  进入交互 psql 工具,创建表:

  CREATE TABLE mytable (

  id varchar(20),

  name varchar(30));

  创建完成后,会获得一条 “CREATED” 的信息,表示创建成功。如今插入一条数据:

  INSERT INTO mytable values(Author, Xu Yongjiu);

  psql 返回 INSERT 18732 1,查询插入是否成功:

  SELECT * FROM MYTABLE;

  退出 psql ,用 q 命令。