import psycopg2 try: connection = psycopg2.connect(user="dbuser", password="12345678", host="127.0.0.1", port="5432", database="rocket") cursor = connection.cursor() postgreSQL_select_Query = "select sitecode, type, country from site limit 10" cursor.execute(postgreSQL_select_Query) print("Selecting rows from mobile table using cursor.fetchall") mobile_records = cursor.fetchall() print("Print each row and it's columns values") for row in mobile_records: print("sitecode = {0} {1} {2}".format(row[1], row[1], row[2] )) except (Exception) as error: print("Error while fetching data from PostgreSQL", error) finally: # closing database connection. if connection: cursor.close() connection.close() print("PostgreSQL connection is closed")