Visual Studio (vs2017) configures C / C + + postgresql9.6.3 development environment

Time:2021-6-8

development environment

Visual Studio 2017[15.2(26430.16)]

Download address:https://www.visualstudio.com/downloads/

Developer download address:https://www.jb51.net/softs/540849.html

PostgreSQL 9.6.3

Download address:https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

Configuration steps

First, download the required version of PostgreSQL from the URL above. There are 32-bit and 64 bit options here. The number of bits here refers to the number of bits of the software you developed by calling PostgreSQL, not the number of bits of your computer.

这里写图片描述

Open the downloaded installation package and click “next” directly in most steps.
In the “password” interface, you can add a password to the default user (the user name is “progressgres”).

这里写图片描述

After the installation, remove the hook and click “finish”.

这里写图片描述

To create a new project, this paper selects the console application (empty project) to facilitate demonstration.

这里写图片描述

Enter project properties.

这里写图片描述

Select the corresponding platform configuration, multiple configurations need to be configured separately.
这里写图片描述

Add the “include” folder under the PostgreSQL installation directory to the project.

 

这里写图片描述
这里写图片描述 

Method, add the “lib” folder to the project.

Add libpq.lib.

Enter “libpq. Lib” manually.

这里写图片描述

Complete the project configuration.
Open the PostgreSQL installation directory.
Copy “libeay32. DLL”, “libiconv-2. DLL”, “libintl-8. DLL” and “ssleay32. DLL” in the “lib” folder to the project directory.
Copy “libpq. DLL” in the “bin” folder to the project directory.

这里写图片描述

Test code

Don’t forget to select the project platform configuration before testing the code

这里写图片描述


#include <libpq-fe.h>

int main () {
 int lib_ver = PQlibVersion ();
 printf ("Version of libpq: %d\n", lib_ver);
 PGconn *conn = PQconnectdb ("host=192.168.1.104 dbname=testdb user=postgres password=abc123");
 if (PQstatus (conn) == CONNECTION_BAD) {
 fprintf (stderr, "Connection to database failed: %s\n",
  PQerrorMessage (conn));
 PQfinish (conn);
 return 0;
 }
 int ver = PQserverVersion (conn);
 printf ("Server version: %d\n", ver);
 PQfinish (conn);
 return 0;
}

Results of operation:

这里写图片描述

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.