PostgreSQL
一、安装配置
1、到http://www.postgresql.org/ftp/binary/v8.1.5/win32/
下载postgresql-8.1.5-1-binaries-no-installer.zip
解压到C盘下(postgresql所在盘的文件系统必须为NTFS)
C:\pgsql
2、设置环境变量path
C:\pgsql\bin;C:\pgsql\lib;
3、在管理工具->计算机管理->本地用户和组->用户下建立一个普通帐号(不具管理员权限)
比如用户名:postgres,密码:postgres
二、创建启动数据库服务器
1、在命令行下以用户postgres运行cmd
runas /user:postgres cmd
键入密码postgres
下面在以用户postgres运行的命令行窗口下操作
2、创建数据库群集
创建超级用户名为system的数据库群集c:\pgsql\data
initdb c:\pgsql\data -Usystem –W
输入密码:manager
查看帮助:initdb --help
3、启动服务器
pg_ctl start -Dc:\pgsql\data
查看帮助:pg_ctl --help
三、在命令行下操作(不必在用户postgres下操作)
1、连接数据库
psql -hlocalhost -p5432 -Usystem -W –dpostgres
(缺省数据库为postgres与启动数据库服务器的用户名相同)
密码:manager
查看帮助:psql --help
连接成功后进入postgres=# 的提示符下操作
2、下面就可以进行对数据库的操作了
比如创建一个数据库
create database demo;(语句结束加“;”)
3、退出当前操作的数据库
\q
四、关闭数据库服务器(在以用户postgres运行的命令行窗口下操作)
pg_ctl stop -Dc:\pgsql\data –mfast
五、图形界面管理工具
到http://www.postgresql.org/ftp/pgadmin3/release/v1.6.0/win32/
下载pgadmin3-1.6.0.zip,解压安装之后,就可在图形界面下对PostgreSQL数据库进行操作。
六、数据库连接配置
1、pg_hba.conf配置
例如:允许来自192.168.x..x 的任何用户与任意数据库联接,且不需要口令
# TYPE DATABASE USER CIDR-ADDRESS METHOD
host all all 192.168.0.0/16 trust
具体可参照PostgreSQL帮助文档
或http://www.pgsqldb.org/pgsqldoc-cvs/client-authentication.html
2、为使以上配置能够成功连接还要对postgresql.conf中的listen_addresses参数进行配置,
声明服务器监听客户端应用连接的TCP/IP地址,缺省值是 localhost,若为listen_addresses = '*',那么服务器将会监听任何IP
具体可参照PostgreSQL帮助文档
或http://www.pgsqldb.org/pgsqldoc-cvs/runtime-config-connection.html
七、jdbc连接PostgreSQL数据库
1、到http://jdbc.postgresql.org/download.html下载jdbc驱动
2、将postgresql-8.1-407.jdbc3.jar放到j2sdk1.4.2_03\jre\lib\ext下面
3、jsp连接PostgreSQL数据库的例子
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>demo</title>
</head>
<body>
<%
Class.forName("org.postgresql.Driver").newInstance();
//url路径格式为jdbc:postgresql://[host]:[port]/[dbname],端口默认为5432,可在postgresql.conf中配置,demo为要连的数据库名
String url ="jdbc:postgresql://localhost/demo";
String user="system";
String password="manager";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select id,city,temp_lo,temp_hi,prcp,date from weather order by id asc";
ResultSet rs=stmt.executeQuery(sql);
%>
<table border="1">
<tr align="center">
<td><b>id</b></td>
<td><b>city</b></td>
<td><b>temp_lo</b></td>
<td><b>temp_hi</b></td>
<td><b>prcp</b></td>
<td><b>date</b></td>
</tr>
<%
while(rs.next())
{
%>
<tr>
<td> <%= rs.getString("id") %></td>
<td> <%= rs.getString("city") %></td>
<td> <%= rs.getString("temp_lo") %></td>
<td> <%= rs.getString("temp_hi") %></td>
<td> <%= rs.getString("prcp") %></td>
<td> <%= rs.getString("date") %></td>
</tr>
<%}%>
</table>
<%
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
参考:http://jdbc.postgresql.org/documentation/80/connect.html
八、参考资料