• 在 Linux 上安装 PostgreSQL


    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>

  • 相关阅读:
    利用百度轻松语音合成,语音识别
    python圆周率计算小程序(非常慢)
    python成语接龙小游戏
    在数组添加元素时报错:IndexError: list index out of range
    Redis-jedis的使用
    Shiro整合SpringMVC简单实例(一)
    容器
    防重提交功能(Token技术的引入)
    PageUtil
    单例设计模式
  • 原文地址:https://www.cnblogs.com/lds85930/p/817449.html
Copyright © 2020-2023  润新知