先程の entry の postgres版。
今回のpostgresも /usr/local/pgsql へ、source install していますので、 通常の pip3 インストールとは、少々、手順が異なります。
https://pypi.org/project/psycopg2/
install psycopg2 for python3
$ sudo su - $ export PATH="/usr/local/pgsql/bin:$PATH" # pip3 install psycopg2 $ sudo vi /etc/ld.so.conf /usr/local/pgsql/lib ##<--ADD $ sudo ldconfig
sample code of psycopg2 for python3
#!/usr/bin/python3 # -*- coding: utf-8 -*- import psycopg2 def main(): db_conn = psycopg2.connect( 'postgresql://testuser:testuser@localhost:5432/springvue') # USER PASSWD DB_NAME db_cur = db_conn.cursor() sql = "select * from test where id in (%s,%s,%s)" #sql = "select * from test" try: db_cur.execute(sql,('1','2','3')) # db_cur.execute(sql) except Exception as e: print(e) return # db_cur.execute(sql) ret_rows = db_cur.fetchall() # connection.commit() for row in ret_rows: print(row) db_cur.close() db_conn.close() if __name__ == '__main__': main()