1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
from sshtunnel import SSHTunnelForwarder
import psycopg2
import psycopg2.extras
with SSHTunnelForwarder(
('跳板机host', 跳板机端口), # 跳板机的host和port
ssh_username="登录跳板机的用户名",
ssh_pkey="/Users/yebangyu/.ssh/id_rsa", # ssh key
remote_bind_address=('数据库host', 数据库端口)) as server: # 数据库的host和port
conn = psycopg2.connect(host=server.local_bind_host, # 注意这里的用法。
port=server.local_bind_port, # 注意这里的用法。
user = 'postgres',
password = 'xxxxxx',
database = 'xxxxxx')
curs = conn.cursor()
sql = """insert into my_tables(id, user_id, create_time) VALUES %s """
values_list = []
value = [(0, 0, "2023-01-01 00:00:00.000")]
values_list.append(value)
try:
psycopg2.extras.execute_batch(curs, sql, values_list, page_size=1000)
conn.commit()
except Exception as e:
print(e)
curs.close()
conn.close()
|