Posted on 2006-10-13 19:27
dybjsun 閱讀(271)
評論(0) 編輯 收藏 所屬分類:
道聽途說 、
Linux專題
MySQL 是一條輕快的小海豚,但是缺少很多現代關系數據庫應有的特色,例如:引用完整性,視圖,觸發器等。因此,如果你需要開發一個電子商務的網站,需要這些功能的話,你或許應該考慮 PostgreSQL 了。本文將通過其在 Red Hat 7.1 上安裝過程,簡要介紹其用法。
PostgreSQL 的官方下載地址為:
ftp://ftp.postgresql.org/pub/v7.1.3/postgresql-7.1.3.tar.gz
http://www.postgresql.org/
如果下載最新的開發版本,你需要下載并安裝 flex(版本號大于 2.5.4) 以及 bison (版本號大于 1.28)
設計人員為了安全考慮,PostgreSQL 不能以 root 用戶運行,所以必須建立對應的用戶和組。
1.
# useradd postgre (自動建立 postgre 組)
2.
安裝的過程并不復雜和其他源碼版本的安裝方法類似:
解壓到 /usr/local/src:
# 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
3.
這樣安裝完畢后,并不是萬事大吉了,還有一些收尾工作要做:
# 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 ) ,并在后臺運行。
4.
建立數據庫
$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 命令。
5.
配置JDBC遠程連接
#vi postgre/data/pg_hba.conf
修改為
host??? all???????? all???????? 192.168.1.1???????? 255.255.255.0??? md5
#vi postgre/data/postgresql.conf
port = 5432
max_connections = 100
shared_buffers = 300 (至少是max_connections的值的兩倍)
listen_addresses = '*'
6.
起動服務
/usr/local/pgsql/bin/pg_ctl -D /data/pgsqldata -l /data/pgsqldata/logfile start
7.
建數據庫
createuser -h 127.0.0.1 -A -D -P test
createdb -h 127.0.0.1? -E UNICODE -O test test
createlang -h 127.0.0.1 plpgsql test
8.
配置tomcat
vi server.conf
在<Host></Host>中間增加
<Context path="/test" docBase="test" debug="4" defaultSessionTimeOut="200" reloadable="true">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_test_log." suffix=".txt" timestamp="true"/>
<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/test">
? <parameter>
??? <name>factory</name>
??? <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
? </parameter>
? <parameter>
??? <name>removeAbandoned</name>
??? <value>true</value>
? </parameter>
? <parameter>
??? <name>removeAbandonedTimeout</name>
??? <value>60</value>
? </parameter>
? <parameter>
??? <name>driverClassName</name>
??? <value>org.postgresql.Driver</value>
? </parameter>
? <parameter>
??? <name>url</name>
??? <value>jdbc:postgresql://192.168.1.5:5432/test</value>
? </parameter>
? <parameter>
??? <name>username</name>
??? <value>test</value>
? </parameter>
? <parameter>
??? <name>password</name>
??? <value>test</value>
? </parameter>
? <parameter>
??? <name>maxActive</name>
??? <value>100</value>
? </parameter>
? <parameter>
??? <name>maxIdle</name>
??? <value>30</value>
? </parameter>
? <parameter>
??? <name>maxWait</name>
??? <value>120</value>
? </parameter>
</ResourceParams>
</Context>
vi web.xml
增加jdbc的連接池
? <resource-ref>
????? <description>DB Connection</description>
????? <res-ref-name>jdbc/test</res-ref-name>
????? <res-type>javax.sql.DataSource</res-type>
????? <res-auth>Container</res-auth>
? </resource-ref>