连接服务

最近更新时间: 2024-10-17 17:10:00

#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"   
int
main(int argc, char **argv){
    const char *conninfo;
    PGconn     *conn;      
    if (argc > 1){
        conninfo = argv[1];
    }else{
        conninfo = "dbname = postgres";  
    }            
    conn = PQconnectdb(conninfo);
    if (PQstatus(conn) != CONNECTION_OK){
        fprintf(stderr, "连接数据库失败: %s",PQerrorMessage(conn));              
    }else{
        printf("连接数据库成功!\n");
    }
    PQfinish(conn);
    return 0;
}

编译

gcc -c -I /usr/local/install/tbase_pgxz/include/ conn.c
gcc -o conn conn.o -L /usr/local/install/tbase_pgxz/lib/ -lpq

运行

./conn "host=172.16.0.3 dbname=postgres port=11000"
#连接数据库成功!
./conn "host=172.16.0.3 dbname=postgres port=15432 user=tbase"
#连接数据库成功!