代码改变世界

CentOS下安装postgresql

2012-06-25 13:47  htc开发  阅读(151)  评论(0编辑  收藏  举报
  • 安装
# yum install postgresql postgresql-libs postgresql-server
[root@dev ~]# service postgresql status
postmaster is stopped
[root@dev ~]# service postgresql start
Initializing database:                                     [  OK  ]
Starting postgresql service:                               [  OK  ]


  • 设为开机启动

[root@dev ~]# chkconfig postgresql on
[root@dev ~]# chkconfig --list postgresql
postgresql      0:off   1:off   2:on    3:on    4:on    5:on    6:off
  • 配置参数

[root@dev ~]# vim /var/lib/pgsql/data/pg_hba.conf
#
 "local" is for Unix domain socket connections only
#
local   all         all                               ident sameuser
local   all         all         trust
# IPv4 local connections:
#
host    all         all         127.0.0.1/32          ident sameuser
host    all         all         127.0.0.1/32          md5
host    all         all         192.168.28.3/32       md5 #必须有后面那个,那个是掩码,否则总报pg_hba.conf语法错
[root@dev ~]# su - postgres
-bash-3.2$ pg_ctl reload
postmaster signaled
[root@dev ~]# vim /var/lib/pgsql/data/postgresql.conf 
listen_addresses = '*'
[root@dev ~]# su - postgres
-bash-3.2$ pg_ctl reload
postmaster signaled
  • 常用命令

# psql -l            列出所有数据库   # psql
=> \l                列出所有数据库
=> \c cake            连接到cake数据库
=> \dt                列出所有tables
=> \d shift            查看shift表结构
=> \di                列出所有indexes
=> \d shift_pkey    列出所有index: shift_pkey
  • 测试java代码

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.ResultSet;
 4 import java.sql.Statement;
 5 
 6 public class PostgresqlTest {
 7 
 8     /**
 9      * @param args
10      */
11     public static void main(String[] args) {
12         try {
13             Class.forName("org.postgresql.Driver").newInstance();
14             String connectUrl = "jdbc:postgresql://192.168.250.231:5432/poem";
15             Connection conn = DriverManager.getConnection(connectUrl,
16                     "poem", "poem");
17             Statement st = conn.createStatement();
18             String sql = "select 1;";
19             ResultSet rs = st.executeQuery(sql);
20             while (rs.next()) {
21                 System.out.println(rs.getInt(1));
22             }
23             rs.close();
24             st.close();
25             conn.close();
26         } catch (Exception e) {
27             e.printStackTrace();
28         }
29     }
30 
31 }
32