C/C++连接Postgresql数据库(1)


最新版本的libpqxx可从链接下载:Libpqxx下载。 所以下载最新版本,并按照以下步骤:

wget http://pqxx.org/download/software/libpqxx/libpqxx-4.0.tar.gz
tar xvfz libpqxx-4.0.tar.gz
cd libpqxx-4.0
./configure
make
make install
Shell

可以启动/重新启动postgres服务器,使用以下命令运行:

[postgres@localhost Documents]$ pg_ctl -D /home/My_Softwares/Postgresql/Data -l logfile restart


C/C++连接到PostgreSQL数据库

以下C代码段显示如何连接到端口5432上本地机器上运行的现有数据库。在这里,使用反斜杠\续行。

// test.cpp
#include 
#include  

using namespace std;
using namespace pqxx;

int main(int argc, char* argv[])
{
   try{
      connection C("dbname=postgres user=postgres password=123 \
      hostaddr=127.0.0.1 port=5432");
      if (C.is_open()) {
         cout << "Opened database successfully: " << C.dbname() << endl;
      } else {
         cout << "Can't open database" << endl;
         return 1;
      }
      C.disconnect ();
   }catch (const std::exception &e){
      cerr << e.what() << std::endl;
      return 1;
   }
}

现在,我们编译并运行上面的程序来连接到数据库postgres,它已经在你的架构中可用,可以使用用户postgres和密码为:123进行访问。 您可以根据数据库设置使用用户名和密码。记住保持-lpqxx-lpq在给定的顺序! 否则,链接器将抱怨关于缺少以“PQ”开头的名称的函数。


[root@localhost Documents]# g++ test.cpp -lpqxx -lpq -o test
/usr/bin/ld: cannot find -lpq
collect2: error: ld returned 1 exit status
[root@localhost Documents]# 
[root@localhost Documents]# g++ $(pkg-config --libs libpqxx) test.cpp -lpqxx -lpq -o test
Package libpqxx was not found in the pkg-config search path.
Perhaps you should add the directory containing `libpqxx.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libpqxx' found
/usr/bin/ld: cannot find -lpq
collect2: error: ld returned 1 exit status
[root@localhost Documents]# 
[root@localhost Documents]# find / -name libpqxx.pc
/home/zlf/Downloads/libpqxx-4.0/libpqxx.pc
find: ‘/run/user/1000/gvfs’: Permission denied
/usr/local/lib/pkgconfig/libpqxx.pc
[root@localhost Documents]# 
[root@localhost Documents]# export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH
[root@localhost Documents]# 
[root@localhost Documents]# g++ $(pkg-config --libs libpqxx) test.cpp -lpqxx -lpq -o test
[root@localhost Documents]# 
[root@localhost Documents]# dir
CreateGroupUser.txt  Environment_Variables.txt	Postgrelsql_Install.txt  SetPasswd.txt	test  test.cpp
[root@localhost Documents]# ./test
./test: error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory
[root@localhost Documents]# 
[root@localhost Documents]# export LD_LIBRARY_PATH=/home/My_Softwares/Postgresql/Install/lib:$LD_LIBRARY_PATH
[root@localhost Documents]# 
[root@localhost Documents]# ./test
could not connect to server: Connection refused
	Is the server running on host "127.0.0.1" and accepting
	TCP/IP connections on port 5432?

[root@localhost Documents]# pg_ctl -D /home/My_Softwares/Postgresql/Data -l logfile start
bash: pg_ctl: command not found...
[root@localhost Documents]# 
[root@localhost Documents]# $PATH
bash: /usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin: No such file or directory
[root@localhost Documents]# 
[root@localhost Documents]# source /etc/profile
[root@localhost Documents]# 
[root@localhost Documents]# $PATH
bash: /usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/My_Softwares/Postgresql/Install/bin: No such file or directory
[root@localhost Documents]# 
[root@localhost Documents]# pg_ctl -D /home/My_Softwares/Postgresql/Data -l logfile 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 Documents]# 
[root@localhost Documents]# su postgres
[postgres@localhost Documents]$ 
[postgres@localhost Documents]$ pg_ctl -D /home/My_Softwares/Postgresql/Data -l logfile start
could not change directory to "/home/zlf/Documents": Permission denied
waiting for server to start.... done
server started
[postgres@localhost Documents]$ 
[postgres@localhost Documents]$ ./test
Opened database successfully: postgres
[postgres@localhost Documents]$ 
[postgres@localhost Documents]$ 

参考:https://www.yiibai.com/postgresql/postgresql_c_cpp.html

你可能感兴趣的:(Postgresql数据库)