Skip to content

Instantly share code, notes, and snippets.

@RICH0423
Last active April 6, 2022 06:37
Show Gist options
  • Select an option

  • Save RICH0423/732d6ca30f67e3f1f6496e1d3ce63d18 to your computer and use it in GitHub Desktop.

Select an option

Save RICH0423/732d6ca30f67e3f1f6496e1d3ce63d18 to your computer and use it in GitHub Desktop.
Testing PostgreSQL connection with Python
import psycopg2
#establishing the connection
conn = psycopg2.connect(
database="postgres", user='postgres', password='12345678', host='127.0.0.1', port= '5432'
)
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
#Executing an MYSQL function using the execute() method
cursor.execute("select version()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print("Connection established to: ",data)
#Closing the connection
conn.close()
@RICH0423

RICH0423 commented Apr 6, 2022

Copy link
Copy Markdown
Author

pip install psycopg2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment