Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import psycopg2 issue
#1
#!/usr/bin/python
import psycopg2
 from config import config
 
	def connect():
    	
	""" Connect to the PostgreSQL database server """
    	conn = None
    	try:
        # read connection parameters
        params = config()
 
        # connect to the PostgreSQL server
        print('Connecting to the PostgreSQL database...')
        conn = psycopg2.connect(**params)
 
        # create a cursor
        cur = conn.cursor()
        
 	# execute a statement
        print('PostgreSQL database version:')
        cur.execute('SELECT version()')
 
        # display the PostgreSQL database server version
        db_version = cur.fetchone()
        print(db_version)
       
     	# close the communication with the PostgreSQL
        cur.close()
    	except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    
	finally:
        if conn is not None:
        	conn.close()
            	print('Database connection closed.')
 
 
	if __name__ == '__main__':
    	connect()

 
while executing above py script I am facing below issue

Quote:MacBook-Pro-4:python_study bkumar$ ./check_connect.py
File "./check_connect.py", line 3
from config import config
Reply
#2
line 3 does not start on column 0, thus perceived as indented.
If you fix this, and still get an error, it's indicative of package not not being installed,

install with:
pip install psycopg2
Note: when posting error traceback, always post entire unmodified error message (in error tags).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Puzzling import issue that I have no idea how to solvr starseeker 3 432 Feb-21-2024, 05:07 PM
Last Post: deanhystad
  How psycopg2 autocommit works ? johntay 3 10,206 Oct-08-2021, 11:22 AM
Last Post: Larz60+
  Psycopg2 doesn't work with python2 MedianykEugene 3 2,888 Aug-10-2021, 07:00 AM
Last Post: ndc85430
  PostgreSQL psycopg2.errors.DuplicateColumn: column specified more than once rajnish_nationfirst 2 3,693 Jun-21-2020, 08:17 AM
Last Post: ibreeden
  tornado psycopg2 Nikosznb 1 2,282 Feb-23-2020, 10:49 PM
Last Post: scidam
  Frequency and timing of psycopg2 commits acecase 0 1,954 Nov-01-2019, 05:50 PM
Last Post: acecase
  Create table with psycopg2 on postgreSQL DB yhecohen 2 3,276 Aug-23-2019, 05:56 AM
Last Post: massimo_m
  psycopg2 insert error Wonder_women 0 2,644 Jun-10-2019, 11:56 AM
Last Post: Wonder_women
  crypto import issue saisankalpj 2 7,784 Dec-20-2018, 06:08 AM
Last Post: saisankalpj
  Need help on a psycopg2 question badfish 1 5,442 Nov-28-2018, 10:16 PM
Last Post: badfish

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020