It is troublesome to check it every time, so I will write it down.
import psycopg2
#Change arbitrarily
postgre_user_name = "user_name"
postgre_user_password = "user_password"
postgre_server = "server"
postgre_server_port = "server_port"
postgre_database_name = "database_name"
postgre_table_name = "table_name"
conn = psycopg2.connect("postgresql://"
+ postgre_user_name
+ ":"
+ postgre_user_password
+ "@"
+ postgre_server
+ ":"
+ postgre_server_port
+ "/"
+ postgre_database_name)
cur = conn.cursor()
sql_sentence = "select count(*) from " + postgre_table_name + ";"
cur.execute(sql_sentence)
cur.fetchall()
The connection destination is converted as follows
postgresql://user_name:user_password@server:server_port/database_name
The sql statement is converted as follows.
select count(*) from table_name;
Recommended Posts