首页 > 数据库 >PgSQL

PgSQL

时间:2023-10-22 19:46:36浏览次数:27  
标签:SET -- NULL public PgSQL t1 id

alter table table_name alter column column_name new_type
CREATE TABLE public.t2 (
    id serial primary key,
    name character varying(40) NOT NULL,
    author character varying(40) NOT NULL,
    comment character varying(40) NOT NULL,
    content character varying(40) NOT NULL,
    isbn character varying(40) NOT NULL,
    object character varying(40) NOT NULL,
    summary character varying(40) NOT NULL,
    topic character varying(40) NOT NULL
);

pgsql auto increment as serial

pg_dump show create table statment

pg_dump -U user_name -h host_name -st table_name db_name
pg_dump -U fred -h localhost -st t1 db


pg_dump -U fred -h localhost -st t1 db;
Password: 
--
-- PostgreSQL database dump
--

-- Dumped from database version 15.4 (Ubuntu 15.4-0ubuntu0.23.04.1)
-- Dumped by pg_dump version 15.4 (Ubuntu 15.4-0ubuntu0.23.04.1)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: t1; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public.t1 (
    id bigint NOT NULL,
    name character varying(40) NOT NULL
);


ALTER TABLE public.t1 OWNER TO postgres;

--
-- Name: t1_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--

CREATE SEQUENCE public.t1_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.t1_id_seq OWNER TO postgres;

--
-- Name: t1_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--

ALTER SEQUENCE public.t1_id_seq OWNED BY public.t1.id;


--
-- Name: t1 id; Type: DEFAULT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.t1 ALTER COLUMN id SET DEFAULT nextval('public.t1_id_seq'::regclass);


--
-- Name: t1 t1_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.t1
    ADD CONSTRAINT t1_pkey PRIMARY KEY (id);


--
-- PostgreSQL database dump complete
--

 

 

show create table

//pg_dump  -st table_name db_name;
pg_dump -st t1 db
--
-- PostgreSQL database dump
--

-- Dumped from database version 15.4 (Ubuntu 15.4-0ubuntu0.23.04.1)
-- Dumped by pg_dump version 15.4 (Ubuntu 15.4-0ubuntu0.23.04.1)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: t1; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public.t1 (
    id bigint NOT NULL,
    name character varying(40) NOT NULL
);


ALTER TABLE public.t1 OWNER TO postgres;

--
-- Name: t1_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--

CREATE SEQUENCE public.t1_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.t1_id_seq OWNER TO postgres;

--
-- Name: t1_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--

ALTER SEQUENCE public.t1_id_seq OWNED BY public.t1.id;


--
-- Name: t1 id; Type: DEFAULT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.t1 ALTER COLUMN id SET DEFAULT nextval('public.t1_id_seq'::regclass);


--
-- Name: t1 t1_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.t1
    ADD CONSTRAINT t1_pkey PRIMARY KEY (id);


--
-- PostgreSQL database dump complete
--

 

 

//create table t2

 CREATE TABLE public.t2 (
    id serial primary key,
    name character varying(40) NOT NULL,
    author character varying(40) NOT NULL,
    comment character varying(40) NOT NULL,
    content character varying(40) NOT NULL,
    isbn character varying(40) NOT NULL,
    object character varying(40) NOT NULL,
    summary character varying(40) NOT NULL,
    topic character varying(40) NOT NULL
);

//c++ insert data into pgsql

#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;
        for (int interval = 0; interval < 10000; interval++)
        {
            pqxx::connection conn("dbname=db user=fred password=Fred0001!");
            pqxx::work trans(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);
            pqxx::result 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;

 

标签:SET,--,NULL,public,PgSQL,t1,id
From: https://www.cnblogs.com/Fred1987/p/17780917.html

相关文章

  • PgSql - PostGIS 在 PostgreSQL 中使用
    首先来介绍一下PostGIS是什么?PostGIS是PostgreSQL对象关系数据库的一个空间数据库扩展。它增加了对地理对象的支持,允许在SQL中运行位置查询。官方介绍:PostGIS:SpatialandGeographicobjectsforPostgreSQLPostGISisaspatialdatabaseextenderforPostgreSQLobject......
  • 转载 https://www.cnblogs.com/star521/p/13385181.html --PGSQL-脏数据清理,频繁de
    查看表大小--查出所有表(包含索引)并排序--查出所有表(包含索引)并排序SELECTtable_schema,table_nameAStable_full_name,pg_size_pretty(pg_total_relation_size('"'||table_schema||'"."'||table_name||'"'))ASsizeFROMinfor......
  • pgsql sql语句参数量太多报错
    批量入库pgsql的时候,发现pgsql一次只能传3万多参数,要么改数据库链接url的参数要么分批次的批量插入。 分批次批量插入: 1intlimit;//要切割成多少份2intMAX_NUMBER;//每份最多多少条数据34List<List<Integer>>splitList=Stream.iterate(0,n->n+1).......
  • pg小工:pgsql介绍
    pg小工:pgsql介绍介绍世界上最先进的开源数据库设计灵活,可定制支持ANSI/ISO兼容的SQL已经有30多年发展历史universitypostgres(1986-1993)postgres95(1994-1995)postgressql(1996-2019)有活跃在全球范围内的社区支持支持邮件列表http://www.postgresql.org/community/lis......
  • 迁移pgsql从数据库(原先数据库架构为主从同步)
    迁移pgsql从数据库将原先的1.56服务器上的从数据库迁移至1.62服务器上55服务器为主库1、安装依赖包yum-yinstallreadlinegcc-yreadline-develzlib-devel2、下载对应版本的pgsql并解压编译安装下载地址:https://www.postgresql.org/ftp/source/tar-xvfpostgresql-11.6......
  • Node 配合 webseket 订阅 pgsql 的数据表变化
    pg订阅传送门服务端代码const{Client}=require('pg');constWebSocket=require('ws');constwss=newWebSocket.Server({port:8080,perMessageDeflate:false,verifyClient:(info,cb)=>{constorigin=info.origin||'......
  • pgsql 查出哪些表有 phone 字段
    --查出哪些表有phone字段selecta.relname表名,b.attname字段名,c.typname字段类型frompg_classa,pg_attributeb,pg_typecwherea.oid=b.attrelidandb.atttypid=c.typelemandb.attname~'phone'andb.attnum>0andb.attisdropped='f';......
  • pgsql备份工具:pg_rman在Linux下的安装、设置与使用
    https://blog.csdn.net/Absurdreal/article/details/128872628?spm=1001.2101.3001.6650.8&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-8-128872628-blog-128017299.235%5Ev38%5Epc_relevant_sort&depth_1-utm_sou......
  • pgsql 把空换成其他值 coalesce函数
    SQL中的in、notin语句遇到null时的坑点_notinnull_shenzhou_yh的博客-CSDN博客 postgres判断null_PGSQL实现判断一个空值字段,并将NULL值修改为其它值_幼生期的博客-CSDN博客......
  • PGSQL_实操常用指南
    1、建表--定额配置主表--1、表不存在->创建--2、给注释CREATETABLEIFNOTEXISTSpublic.pro_salary_quota(idvarchar(50)NOTNULL,--主键Idworking_process_typeint4NOTNULL,--工序类型:1-构件工序,2-零件工序working_process_idvarch......