ubuntu install pgsql

sudo apt update;
sudo apt upgrade;
sudo apt install postgresql postgresql-contrib;
sudo systemctl start postgresql.service

https://www.digitalocean.com/community/tutorials/how-to-install-postgresql-on-ubuntu-20-04-quickstart

sudo -i -u postgres psql

//
show databases \l

//switch database
\c db_name

 

 
psql -h localhost -d db_name -U user_name;

 

sudo apt install libpqxx-6.4
sudo apt install
libpqxx-dev

 

复制代码
#include <iostream>
#include <pqxx/pqxx>

int main()
{
    try
    {
        pqxx::connection c("dbname=db_name user=user_name password=password_value);
        pqxx::work w (c);
        pqxx::result res=w.exec("SELECT * from t1;");
        w.commit();
        int rows_count=res.size();
        pqxx::row_size_type rst=res.columns();
        for(int i=0;i<rows_count;i++)
        {
            for(int j=0;j<rst;j++)
            {
                std::cout<<res[i][j]<<"\t";
            }
            std::cout<<std::endl;
        }  
    }
    catch (std::exception const &e)
    {
        std::cerr << "ERROR: " << e.what() << '\n';
        return 1;
    }
}
复制代码
g++-13 -std=c++23 -I.  main.cpp -lpqxx -lpq -o h1;
./h1
1    ef9f9112-e727-41c8-8d2e-a41c88f677f1    
2    972476e9-9f95-4d3f-8ed6-8b101d58f85d    
3    7fb01d01-5b9c-4569-a211-e305be9f084b

 

 

 

复制代码
#include <iostream>
#include <algorithm>
#include <chrono>
#include <ctime>
#include <fstream>
#include <hashtable.h>
#include <iomanip>
#include <memory>
#include <mutex>
#include <queue>
#include <random>
#include <sstream>
#include <set>
#include <thread>
#include <time.h>
#include <uuid/uuid.h>
#include <vector>
#include <pqxx/pqxx>

std::string get_time_now(bool is_exact = true)
{
    auto 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 << "_";
        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();
}

char *uuid_value = (char *)malloc(40);
char *get_uuid_value()
{
    uuid_t new_uuid;
    uuid_generate(new_uuid);
    uuid_unparse(new_uuid, uuid_value);
    return uuid_value;
}

void insert_into_pg_table()
{
    try
    {
        std::uint64_t num = 0;
        int loops = 0;
        std::stringstream ss;
        std::string insert_sql;
        pqxx::result res;
        std::string conn_str = "dbname=db user=fred password=Fred0001!";
        for (int interval = 0; interval < 10000; interval++)
        {
            {
                pqxx::connection conn = pqxx::connection(conn_str);
                pqxx::work trans = pqxx::work(conn);
                res = trans.exec("SELECT * from t2 order by id desc limit 1");
                trans.commit();
                int rows_count = res.size();
                pqxx::row_size_type cols_count = res.columns();
                std::cout << "The latest row: " << std::endl;
                for (int i = 0; i < rows_count; i++)
                {
                    for (int j = 0; j < cols_count - 1; j++)
                    {
                        std::cout << res[i][j] << ",";
                    }
                    std::cout << res[i][cols_count - 1] << std::endl;
                }
            }

            pqxx::connection conn = pqxx::connection(conn_str);
            pqxx::work trans = pqxx::work(conn);
            ss = std::stringstream();
            ss << "insert into t2(name,author,comment,content,isbn,object,summary,topic) values ";
            for (int i = 0; i < 1000000; i++)
            {
                ss << "('" << get_uuid_value() << "','" << get_uuid_value() << "','" << get_uuid_value() << "','"
                   << get_uuid_value() << "','" << get_uuid_value() << "','" << get_uuid_value() << "','"
                   << get_uuid_value() << "','" << get_uuid_value() << "'),";
                ++num;
            }
            insert_sql = ss.str();
            int last_comma_idx = insert_sql.find_last_of(',');
            insert_sql = insert_sql.substr(0, last_comma_idx);
            res = trans.exec(insert_sql);
            trans.commit();
            std::cout << "Num:" << num << ",loops:" << ++loops << std::endl;
        }
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';
    }
}

int main(int args, char **argv)
{
    // thread_detach_for_seconds(atoi(argv[1]));
    insert_into_pg_table();
    std::cout << get_time_now() << ", finished in " << __LINE__ << " of " << __FUNCTION__ << std::endl;
}
复制代码

 

 

g++-13 -g -std=c++23 main.cpp -luuid -lpthread -lpqxx -o h2;

 

./h2

 

 

 

 

posted @   FredGrit  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2021-10-16 C++ generate in Ubuntu
点击右上角即可分享
微信分享提示