python连接pgsql数据库操作
1 import psycopg2 2 #连接数据库 3 conn = psycopg2.connect(database='test',user='postg', 4 password='y0',host='10.13.108.5',port='5432') 5 6 cursor = conn.cursor() #创建一个游标 7 8 #execuet方法是针对数据库的请求 9 cursor.execute('create table test_1(id int,name varchar(20) )') 10 cursor.execute("insert into test_1 values(1,'lg')") 11 cursor.execute("drop table test_1") 12 13 conn.commit() #提交 14 cursor.execute("select * from test_1") 15 rows = cursor.fetchall() #返回所有行的元组 16 for row in rows: 17 print(row[0],row[1]) 18 cursor.close() 19 conn.close()