postgresql-9.1.9编译及简单CGI访问数据库

前端之家收集整理的这篇文章主要介绍了postgresql-9.1.9编译及简单CGI访问数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1:编译

linux直接执行以下命令:
./configure --prefix=/work/postgresql-9.1.9/binpost
当进行交叉编译的时如下:我的编译是有些问题直接跳过了下面俩个包的检测
./configure --host=arm-none-linux-gnueabi --prefix=/work/postgresql-9.1.9/binpost --without-readline --without-zlib

2:创建数据库目录及用户


创建用户postgresql
roupadd postgresql
useradd -g postgresql postgresql
创建数据库目录和日志目录
mkdir /usr/local/pgsql/data
mkdir -p /usr/local/pgsql/log
touch /usr/local/pgsql/log/pgsql.log
改变属主:
chown -R postgresql:postgresql /usr/local/pgsql/data
chown -R postgresql:postgresql /usr/local/pgsql/log
chown -R postgresql:postgresql /usr/local/pgsql/log/pgsql.log

3:初始化及创建数据库
su - postgresql
initdb -D /usr/local/pgsql/data
启动数据库:pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/log/pgsql.log start
[root@localhost /work/postgresql-9.1.9]$pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/log/pgsql.log start
pg_ctl: cannot be run as root
Please log in (using,e.g.,"su") as the (unprivileged) user that will
own the server process.
[root@localhost /work/postgresql-9.1.9]$
上面是因为用户的原因,故需要切换到之前创建的用户postgresql
当切换用户之后:启动数据库
postgresql@localhost ~]$ pg_ctl -D /usr/local/pgsql/data -l /usr/local/pgsql/log/pgsql.log start
server starting

4:创建database及tables
创建数据库用户
createuser -sADEP psmpAdmin
访问数据库: psql -d psmp -U psmpAdmin
[postgresql@localhost ~]$ psql -d psmp -U psmpAdmin
psql (8.4.1)
Type "help" for help.
psmp=> \d
List of relations
Schema | Name | Type | Owner
--------+---------+-------+-----------
public | company | table | psmpAdmin
(1 row)

psmp=> \l
List of databases
Name | Owner | Encoding | Collation | Ctype | Access privileges
-----------+------------+----------+-------------+-------------+---------------------------
postgres | postgresql | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
psmp | postgresql | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgresql | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgresql
: postgresql=CTc/postgresql
template1 | postgresql | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgresql
: postgresql=CTc/postgresql
(4 rows)


psmp=> \c psmp //链接psmp数据库
psql (8.4.1)
You are now connected to database "psmp".
psmp=> select * from COMPANY;
id | name | age | address | salary | join_date
----+-------+-----+----------------------------------------------------+--------+------------
3 | Teddy | 23 | Norway | 20000 |
1 | Paul | 32 | California | 20000 | 2001-07-13
(2 rows)


psmp=>
=====================================================================================================
CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL,
JOIN_DATE DATE
);

drop table company;

INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (1,'Paul',32,'California',20000.00,'2001-07-13');


INSERT INTO COMPANY (ID,JOIN_DATE) VALUES (3,'Teddy',23,'Norway',DEFAULT );


SELECT * FROM COMPANY;


5:CGI代码访问数据库
  1. #include "libpq-fe.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. static void exit_nicely(PGconn *conn)
  5. {
  6. PQfinish(conn);
  7. exit(1);
  8. }
  9. int main()
  10. {
  11. int i,nFields,j;
  12. struct pg_conn * conn = 0;
  13. PGresult *res = 0;
  14. printf("%s\r\n\r\n","Content-Type:text/html");
  15. conn = PQsetdbLogin("localhost","5432","","psmp","psmpAdmin","suiyuan");//succedd
  16. if(PQstatus(conn) != CONNECTION_OK)
  17. {
  18. printf("connect fail \n");
  19. }
  20. else
  21. {
  22. printf("connect success\n");
  23. }
  24. res = PQexec(conn,"BEGIN");
  25. if(PQresultStatus(res) != PGRES_COMMAND_OK)
  26. {
  27. printf("execute sql fail %s",PQerrorMessage(conn));
  28. PQclear(res);
  29. exit_nicely(conn);
  30. }
  31. PQclear(res);
  32.  
  33. res = PQexec(conn,"DECLARE myportal CURSOR FOR select * from company");
  34. if (PQresultStatus(res) != PGRES_COMMAND_OK)
  35. {
  36. printf("DECLARE CURSOR Failed: %s",PQerrorMessage(conn));
  37. PQclear(res);
  38. exit_nicely(conn);
  39. }
  40. PQclear(res);
  41. res = PQexec(conn,"FETCH ALL in myportal");
  42. if (PQresultStatus(res) != PGRES_TUPLES_OK)
  43. {
  44. printf("FETCH ALL Failed: %s",PQerrorMessage(conn));
  45. PQclear(res);
  46. exit_nicely(conn);
  47. }
  48. nFields = PQnfields(res);
  49. printf("<table><font color=\"CC0000\" size=+2>%s</font>");
  50. printf("<tr>");
  51. for (i = 0; i < nFields; i++)
  52. {
  53. //printf("%-15s",PQfname(res,i));
  54. printf("<td>%s</td>",i));
  55. }
  56. printf("</tr>");
  57. printf("\n\n");
  58.  
  59. for (i = 0; i < PQntuples(res); i++)
  60. {
  61. printf("<tr>");
  62. for (j = 0; j < nFields; j++)
  63. {
  64. // printf("%-15s",PQgetvalue(res,i,j));
  65. printf("<td>%s</td>",j));
  66. }
  67. printf("</tr>");
  68. }
  69. printf("</table>");
  70.  
  71. PQclear(res);
  72. /*
  73. res = PQexec(conn,"delete from test where name = 'dog'");
  74. PQclear(res);
  75. */
  76. res = PQexec(conn,"CLOSE myportal");
  77. PQclear(res);
  78. res = PQexec(conn,"END");
  79. PQclear(res);
  80. PQfinish(conn);
  81. return 0;
  82. }




运行CGI程序如下:


在运行CGI的时候,需要首先 httpd 运行。CGI程序放置的位置如下:

猜你在找的Postgre SQL相关文章