PostgreSQL_数据回退,数据库导出、导入

目录

前置:

[1 数据回退](#1 数据回退)

[1.1 代码](#1.1 代码)

[1.2 pgAdmin4 中查看](#1.2 pgAdmin4 中查看)

1)t_daily

2) t_stock_daily t_stock_daily)

[2 数据库导出、导入](#2 数据库导出、导入)


前置:

本博文是一个系列。在本人"数据库专栏"-》"PostgreSQL_"开头的博文。

1 数据回退

上一节"PostgreSQL_数据下载并保存(psycopg2)",保存了 2023年7月11日 至 2025年2月13日 的数据。任意取一个日期,比如2025年1月10日,回退到这个日期。

1.1 代码

复制代码
def rollback_to_day_before(date_str:str):
    conn = connect_db()
    '''
    t_stock_daily
    1 找出每只股票 date_str 对应的索引位置
    2 遍历 update 表格
    '''
    cur = conn.cursor()

    # 传入的 date_str 可能不是交易日,如果不是交易日要找到最靠近 date_str 之前的交易日
    sql_q_m_str = f"select max(tradeDateOj) from t_daily where tradeDateOj <= \'{date_str}\';"
    cur.execute(sql_q_m_str)
    res0 = cur.fetchone()
    date_str0 = res0[0].strftime('%Y-%m-%d')

    sql_query_str = f"select ticker,array_position(tradeDate,\'{date_str0}\') from t_stock_daily;"
    cur.execute(sql_query_str)
    res = cur.fetchall()

    sql_update_str = """
    update t_stock_daily set tradeDate=tradeDate[1:%s],openPrice=openPrice[1:%s],highestPrice=highestPrice[1:%s],lowestPrice=lowestPrice[1:%s],closePrice=closePrice[1:%s],turnoverVol=turnoverVol[1:%s],turnoverValue=turnoverValue[1:%s],dealAmount=dealAmount[1:%s],turnoverRate=turnoverRate[1:%s],negMarketValue=negMarketValue[1:%s],marketValue=marketValue[1:%s],chgPct=chgPct[1:%s],PE=PE[1:%s],PE1=PE1[1:%s],PB=PB[1:%s],isOpen=isOpen[1:%s],vwap=vwap[1:%s] where ticker=%s;
    """

    # 删除 t_daily date_str之后的数据
    sql_delete_str = f"delete from t_daily where tradeDateOj>\'{date_str}\';"

    data_list = []
    for one in res:
        one_node = (
            one[1],
            one[1],
            one[1],
            one[1],
            one[1],
            one[1],
            one[1],
            one[1],
            one[1],
            one[1],
            one[1],
            one[1],
            one[1],
            one[1],
            one[1],
            one[1],
            one[1],
            one[0]
        )
        data_list.append(one_node)
        pass

    try:
        cur.executemany(sql_update_str,data_list)
        cur.execute(sql_delete_str)
        conn.commit()
    except Exception as e:
        print(f'error:{e}')
        conn.rollback()
    finally:
        cur.close()
        conn.close()
    pass

1.2 pgAdmin4 中查看

1)t_daily

2) t_stock_daily

select tradeDate[array_length(tradeDate,1)],openPrice[array_length(openPrice,1)],highestPrice[array_length(highestPrice,1)],lowestPrice[array_length(lowestPrice,1)],closePrice[array_length(closePrice,1)],turnoverVol[array_length(turnoverVol,1)],turnoverValue[array_length(turnoverValue,1)],dealAmount[array_length(dealAmount,1)],turnoverRate[array_length(turnoverRate,1)],negMarketValue[array_length(negMarketValue,1)],marketValue[array_length(marketValue,1)],chgPct[array_length(chgPct,1)],PE[array_length(PE,1)],PE1[array_length(PE1,1)],PB[array_length(PB,1)],isOpen[array_length(isOpen,1)],vwap[array_length(vwap,1)] from t_stock_daily where ticker='000001';

2 数据库导出、导入

导出

1 打开cmd

2 cd 到postgreSQL安装目录的bin目录

执行

pg_dump -U postgres -h 127.0.0.1 -p 5432 -F c -b -v -f E:/db_stock.dump db_stock

注意:"E:/db_stock.dump"换成自己的路径; db_stock换成要导出的数据库名
导入

1 打开cmd

2 cd 到postgreSQL安装目录的bin目录

执行

dropdb -U postgres -h 127.0.0.1 -p 5432 db_stock

createdb -U postgres -h 127.0.0.1 -p 5432 db_stock

pg_restore -U postgres -h 127.0.0.1 -p 5432 -d db_stock -v D:/db_stock.dump

相关推荐
a努力。12 小时前
国家电网Java面试被问:最小生成树的Kruskal和Prim算法
java·后端·算法·postgresql·面试·linq
l1t13 小时前
NineData第三届数据库编程大赛:用一条 SQL 解数独问题我的参赛程序
数据库·人工智能·sql·算法·postgresql·oracle·数独
利兄的视界18 小时前
一步到位:M4 芯片 Mac 安装 PostgreSQL 16 并适配 pgvector 教程
后端·postgresql
oMcLin1 天前
如何在 CentOS Stream 9 上配置并优化 PostgreSQL 15,支持高并发的数据插入与快速查询?
linux·postgresql·centos
Lupino1 天前
构建现代化的 Python PostgreSQL 工具库:psql_utils 的重构与优化之旅
python·postgresql
Chef_Chen1 天前
数据科学每日总结--Day41--ubuntu安装tailscale
数据库·ubuntu·postgresql
赵渝强老师1 天前
【赵渝强老师】国产金仓数据库的模式
数据库·postgresql·oracle·国产数据库
柠檬叶子C1 天前
PostgreSQL 忘记 postgres 密码怎么办?(已解决)
数据库·postgresql
光蛋2 天前
PostgreSQL 显式锁定:从原理到实践(趣味版)
postgresql
2301_800256112 天前
数据库设计中的 “数据依赖→设计异常→关系分解(范式)” 核心逻辑
数据库·postgresql