• 首页
  • css3教程
  • html5教程
  • jQuery手册
  • php7教程
  • MySQL手册
  • apache手册
  • 位置: php7教程 -> php7外部扩展库

    PostgreSQL数据库

    PostgreSQL是一个功能强大的开源对象关系型数据库系统,他使用和扩展了SQL语言,并结合了许多安全存储和扩展最复杂数据工作负载的功能。

    要使用PostgreSQL支持,需要PostgreSQL 6.5或更高版本、PostgreSQL 8.0或更高版本才能启用所有PostgreSQL模块功能。PostgreSQL支持多种字符编码,包括多字节字符编码。

    PostgreSQL模块中使用了两种资源类型。第一个是数据库连接的链接标识符,第二个是保存查询结果的资源。

    安装

    为添加 PostgreSQL 支持,在编译 PHP 时需要加上--with-pgsql[=DIR]选项。如果可以用共享模块方式,PostgreSQL 模块可以在 php.ini 用 extension 指令或者 dl() 函数加载。

    On a Windows server, configured with Apache, adding the following line to httpd.conf to load libpq.dll can save you a lot of time :
    LoadFile "C:/Program Files/PostgreSQL/8.4/bin/libpq.dll"
    Note that you will have to change your folder accordingly to the installation path and version of PostgreSQL you have installed. Also note that having Apache and PostgreSQL on the same server for production environments is not recommended.
    Cheers,
    Serjik
    
    An easy way to install in ubuntu(debain)
    Just run " apt-get install php5-pgsql "
    

    运行时配置

    这些函数的行为受php.ini中的设置影响。

    PostgreSQL 配置选项
    名字默认可修改范围更新日志
    pgsql.allow_persistent"1"PHP_INI_SYSTEM
    pgsql.max_persistent"-1"PHP_INI_SYSTEM
    pgsql.max_links"-1"PHP_INI_SYSTEM
    pgsql.auto_reset_persistent"0"PHP_INI_SYSTEM从 PHP 4.2.0 起开始存在
    pgsql.ignore_notice"0"PHP_INI_ALL从 PHP 4.3.0 起开始存在
    pgsql.log_notice"0"PHP_INI_ALL从 PHP 4.3.0 起开始存在
    pgsql.allow_persistentboolean

    是否允许持久的 Postgres 连接。

    pgsql.max_persistentinteger

    每个进程所能有的持久 Postgres 连接数目。

    pgsql.max_linksinteger

    每个进程所能有的 Postgres 连接数目,包括持久连接。

    pgsql.auto_reset_persistentinteger

    检测用在 pg_pconnect() 上的中断了的持久连接。需要一些损耗。

    pgsql.ignore_noticeinteger

    是否忽略 PostgreSQL 后端的通告。

    pgsql.log_noticeinteger

    是否记录 PostgreSQL 后端的通告消息。要记录通告消息日志,PHP 指令 pgsql.ignore_notice 必须为 off。

    例子

    <?php
    // Connecting, selecting database
    $dbconn = pg_connect("host=localhost dbname=publishing user=www password=foo")
        or die('Could not connect: ' . pg_last_error());
    // Performing SQL query
    $query = 'SELECT * FROM authors';
    $result = pg_query($query) or die('Query failed: ' . pg_last_error());
    // Printing results in HTML
    echo "<table>\n";
    while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
        echo "\t<tr>\n";
        foreach ($line as $col_value) {
            echo "\t\t<td>$col_value</td>\n";
        }
        echo "\t</tr>\n";
    }
    echo "</table>\n";
    // Free resultset
    pg_free_result($result);
    // Closing connection
    pg_close($dbconn);
    ?>
    
    <?php
    // This function should be needed, since PostgreSQL connection binds database.
    function pg_list_dbs($db)
    {
        assert(is_resource($db));
        $query = '
    SELECT
     d.datname as "Name",
     u.usename as "Owner",
     pg_encoding_to_char(d.encoding) as "Encoding"
    FROM
     pg_database d LEFT JOIN pg_user u ON d.datdba = u.usesysid
    ORDER BY 1;
    ';
        return pg_query($db, $query);
    }
    // List tables.
    function pg_list_tables($db)
    {
        assert(is_resource($db));
        $query = "
    SELECT
     c.relname as \"Name\",
     CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' END as \"Type\",
      u.usename as \"Owner\"
    FROM
     pg_class c LEFT JOIN pg_user u ON c.relowner = u.usesysid
    WHERE
     c.relkind IN ('r','v','S','')
     AND c.relname !~ '^pg_'
    ORDER BY 1;
    ";
        return pg_query($db, $query);
    }
    // See also pg_meta_data(). It returns field definition as array.
    function pg_list_fields($db, $table)
    {
        assert(is_resource($db));
        $query = "
    SELECT
     a.attname,
     format_type(a.atttypid, a.atttypmod),
     a.attnotnull,
     a.atthasdef,
     a.attnum
    FROM
     pg_class c,
     pg_attribute a
    WHERE
     c.relname = '".$table."'
     AND a.attnum > 0 AND a.attrelid = c.oid
    ORDER BY a.attnum;
    ";
        return pg_query($db, $query);
    }
    ?>