1.Install libpq-dev
sudo apt install libpq-dev
locate libpq-fe.h /usr/include/postgresql/libpq-fe.h
2.main.cpp
#include <chrono> #include <fstream> #include <iomanip> #include <iostream> #include <sstream> #include <thread> #include <vector> #include <uuid/uuid.h> #include "libpq-fe.h" std::string get_time_now(bool is_exact = true) { std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now(); time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now); struct tm tm_info = *localtime(&raw_time); std::stringstream ss; ss << std::put_time(&tm_info, "%Y%m%d%H%M%S"); if (is_exact) { auto seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()); auto mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()); auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()); auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()); ss << std::setw(3) << std::setfill('0') << (mills.count() - seconds.count() * 1000) << std::setw(3) << std::setfill('0') << (micros.count() - mills.count() * 1000) << std::setw(3) << std::setfill('0') << (nanos.count() - micros.count() * 1000); } return ss.str(); } // create table t1(id bigserial not null primary key,author varchar(40) not null,comment varchar(40) not null,content varchar(40) not null,header varchar(40) not null,isbn varchar(40) not null,object varchar(40) not null,summary varchar(40) not null); void connect_psql() { int nfields; std::string conn_str = "host=host_value port=port_num_value user=user_name_value password=password_value dbname=dbname_value"; const char *conn_char_ptr = conn_str.c_str(); PGconn *conn = PQconnectdb(conn_char_ptr); // std::cout << PQstatus(conn) << std::endl; if (PQstatus(conn) == CONNECTION_OK) { std::cout << "ConnStatusType:CONNECTION_OK" << std::endl; } else if (PQstatus(conn) == CONNECTION_BAD) { std::cout << "ConnStatusType:CONNECTION_BAD" << std::endl; } const char *select_str = "select * from t1"; PGresult *res = PQexec(conn, select_str); if (PQresultStatus(res) != PGRES_TUPLES_OK) { std::cout << "Exec " << select_str << " failed!" << std::endl; PQclear(res); } nfields = PQnfields(res); std::cout << std::endl; for (int i = 0; i < PQntuples(res); i++) { for (int j = 0; j < nfields - 1; j++) { std::cout << PQfname(res, j) << ":" << PQgetvalue(res, i, j) << ","; } std::cout << PQfname(res, nfields - 1) << ":" << PQgetvalue(res, i, nfields - 1) << std::endl << std::endl; } PQclear(res); // res=PQexec(conn,"END"); // PQclear(res); PQfinish(conn); std::cout << get_time_now() << ",finished line:" << __LINE__ << " of " << __FUNCTION__ << std::endl; } int main(int args, char **argv) { // -I/usr/include/postgresq // g++-13 -std=c++23 -I. main.cpp -I/usr/include/postgresql -lpq -o h1; connect_psql(); std::cout << get_time_now() << ",finished line:" << __LINE__ << " of " << __FUNCTION__ << std::endl; }
Compile
g++-13 -std=c++23 -I. main.cpp -I/usr/include/postgresql -lpq -o h1;
Run
./h1
sudo psql -h localhost -p 5432 -Ufred -ddb Password for user fred: psql (15.4 (Ubuntu 15.4-1ubuntu1)) SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off) Type "help" for help. db=> \d+ List of relations Schema | Name | Type | Owner | Persistence | Access method | Size | Description --------+-----------+----------+----------+-------------+---------------+------------+------------- public | t1 | table | postgres | permanent | heap | 8192 bytes | public | t1_id_seq | sequence | postgres | permanent | | 8192 bytes | (2 rows) db=> select * from t1;
id | author | comment | content | header | isbn | object | summary ----+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------+-------------------------------------- 1 | 832002ce-70ac-43af-89fc-5216710e4125 | 2931bbe1-d8b0-4253-9d2d-389bcdf217be | da383e5b-3fa3-49b5-8635-29967749bf23 | 3b378f54-3ddb-458d-9103-a720357cbc64 | 711dd85b-02d0-4cb2-8c5e-dabfe48ef8d2 | 33940627-94ec-41d1-ac68-7919ddfa2061 | ef8074fd-c88e-4c5b-94a3-6b67d5546568 2 | 6154e778-3822-4254-aac8-36bcacb6f379 | c275d426-fbd3-432b-b93c-0b45b13d8f87 | 53608fb7-e5ff-462c-b42f-6643bf340035 | 58bc70c6-e9b1-4434-9106-502c10a0cbee | d76495d4-cd22-43aa-90d8-9a9ed2321758 | db515381-bd7d-47f6-8aad-6c7f23f70732 | 7e9b5764-cb3a-4367-9d50-903ce8d80656 3 | 0aa4a16d-e5ec-4f8e-8927-d6f5556ecfe4 | 3a5b2392-c525-4b0f-8243-7e07b3667877 | f6249242-63a6-4211-aec4-7f7dbf5dccee | 4dc19d00-2eca-4a61-b44d-4e3ba97c0af5 | 07bc21a1-0e61-48de-8ac1-9247abc7c483 | b057f8b7-9a06-4e36-bc3c-1bdaa9a2dcff | e936a8aa-29a4-4768-b467-a3cf2598a425 4 | d901f701-e754-402e-a71b-0741fe536f13 | ea37b048-eb10-444a-aa6d-893d006209b5 | d7216638-a65f-4697-917f-1c4d9c4fc038 | 8627c54d-7707-4a51-acea-595c6a52f6d0 | ba394600-9aa0-45c8-93d6-308548c677d2 | adbf7a94-c225-4c8e-ae07-c7d85d83da60 | 67fface6-ede0-4b38-9466-5416a4ab1ca8 5 | 67777ee7-4eb8-4991-9695-dc4ad663d02f | 27b3f7dc-c300-4e78-8449-7e41c7a01e6f | 57042577-e621-49a2-a2d8-808bcb6e9a70 | 109be28a-10b1-4e39-884a-952e13205991 | 23b0bd67-ce3d-4df8-a04a-f45b4312a008 | 19d28ceb-1e61-4351-b8a8-aed965ad857c | 5d5afe95-0915-4515-bdef-b22ec642b78b 6 | 139397b7-3b95-43a3-aa7c-40503cab4e82 | 2ce09eef-9b32-4aef-86b4-c06bfc7a8717 | 11f89fcf-5927-4631-9fe9-95d53357f761 | db83c425-b830-41c3-b229-cdc7d813e60b | a6d09fa0-ec46-4f4a-9fac-eac9e7119577 | f83bb30b-462f-4492-b5e1-82be2541d130 | 367d79ab-d903-43df-9ad9-02aa5a415e03 7 | 9767e761-5bb0-4c16-b198-83e0c0a7117f | 9a723a58-4bfd-413b-9a32-230988155059 | 54ae24da-91ed-4669-9c74-f405f442f488 | 1cc89d76-6361-4825-8aa4-3b08a2bbe76e | 4a6b3273-5f89-4f9a-b6cb-322e03df941a | bcf8f936-f768-4400-817b-d035721b1d9e | ce9545f9-6cb4-480c-b3e9-ed5eabaaa6ee 8 | 3e1118e2-e3ed-48de-86a7-3927e1eeb7c1 | 0d2c46d0-2a47-45cd-b9d9-d7aff296e62e | aabe5d70-2ae7-41e6-b1e3-44d58084de2c | ef846003-0305-4197-9f2b-be537f8b08d3 | 4825a55f-a9a8-4eb9-8bd2-66080c24b111 | 06d1a76f-24bc-4f80-a862-5daf31034589 | c7412eed-5d8b-40b4-a712-ef002bc83a52 9 | a7763428-29c6-4ffc-b8d3-209f71cc7c2b | 7ac13833-768f-430c-aa51-34f87a167ce3 | c05c1fb7-5152-408d-9bb9-30eea6300f62 | 7487bcc1-6428-4538-8a38-690fdc89e7af | 4d62f1b7-8e3b-409c-8756-3697520431cb | 36d304ba-19af-4413-8c27-65f8438fea77 | d473afc9-2687-4f3e-8373-24b467db491a 10 | c6aa9dd5-2d2a-4340-a2fd-b58b5d6cf462 | 68216312-acc5-406d-8579-542e6c74b98e | f183f06b-434b-49b3-ace6-f58544dc5008 | 3bcb9b41-7f28-4670-8e81-0283badd79a8 | 9c8d5009-2331-4aa5-bf6e-917ee79a848c | 12ec3baa-cfd6-4dee-8414-7cf1ed98d48d | a240604c-9a7a-4e7a-9bae-f9d4797cd0b1 11 | f3b9a463-319f-44b2-ab69-948d00fb730d | 0c4cf290-ffcc-48fb-b778-5ec525a0aa6b | d0f8c2a7-483f-4b3e-b3ff-8bf32c57d8ee | defcdc29-914a-4fb4-aa21-9f91893fc8ed | ae00b78d-b383-4cd6-bad7-65ae3298321f | ea1c1057-62e7-4770-9f25-8e0b6c18013b | 49ef3a60-acd7-43b3-bea9-24abbaf0be8c 12 | 13c37e71-f034-44de-9864-11370eb9d8a1 | 4f5c5e68-74d8-42b3-aca5-46e74b2ea1fb | 759b8d71-e4b2-4cfc-9c02-a0588c79a1b3 | def77e11-7e3e-4605-ab2f-947f44db7e91 | 02e733da-80c7-4fe9-ba50-5a9682272f2d | 7b21eb68-98fb-470f-92c8-d792b7f30c12 | b49ab842-f4f2-4f45-a8fe-0b3beb036c89 13 | 331a9315-72d6-4f33-bdc0-a58272cdb3c7 | 6394d399-b08c-4b13-adca-f1b82b38928a | 07f7cd69-8d7c-4704-8562-24715fdf2a9d | 3b43f988-cc53-44a7-b1eb-d5bc335e765a | be8fd9b1-06cb-4009-b730-9deaa123c7d8 | c887a617-fb24-42de-9c22-dd7932c6dc74 | 9dc24c75-7f76-4dbf-a475-9615bdadee96 14 | dd149a8b-6f90-401e-8b03-83ca3a21aa1b | 987ccc6f-09a3-41f1-9102-ea4d3f335f54 | 5d552896-7ae8-46b1-abb7-634b268a961a | ddf28e8a-8c5e-4867-b883-d4c2dc037600 | bba96ef9-7e71-4b0f-a98e-582c1b7b9850 | 8807d080-18c7-4fd0-a206-b00210d12870 | 12a0dfdb-5ec6-4b36-8228-8cab8bdc98b8 15 | e3532fe6-4624-4626-869e-db926562fb26 | b06a18ba-28d3-498d-87d7-221a77585aef | 5b904b44-4463-43de-8afb-4bf4a95abafc | 0dfe2bd2-2be3-4084-8608-687fa79cae30 | 76969e08-47d2-46e2-ab0b-2a4fb02264b3 | 389f93b7-e2bc-4f5e-af23-5ce37f74265d | 79058600-864e-4672-bd8f-8352c6fee82a 16 | 31d7f3fa-8431-4b3d-bb53-a124b5187841 | 6cb9d002-3373-4daf-a7f3-908d970ef071 | 733ce1cd-e4a7-4054-b4f7-0b32bbd8c3c2 | a0cd5bde-4e09-412c-8c31-5165b2194bef | bca0ac33-e352-405b-8fd3-51cef3e24af7 | 254a4cde-5952-4d46-a38e-cbaae8e26fee | 3e55ef6e-c4f8-4236-bb18-99d4fa64f835 (16 rows)
标签:PQconnectdb,std,now,PGconn,PGresult,chrono,libpq,time,include From: https://www.cnblogs.com/Fred1987/p/17823000.html