Answers for "accessing database with python postgresql"

5

how to connect postgres database to python

import psycopg2
try:
    connection = psycopg2.connect(user = "sysadmin",
                                  password = "pynative@#29",
                                  host = "127.0.0.1",
                                  port = "5432",
                                  database = "postgres_db")

    cursor = connection.cursor()
    # Print PostgreSQL Connection properties
    print ( connection.get_dsn_parameters(),"\n")

    # Print PostgreSQL version
    cursor.execute("SELECT version();")
    record = cursor.fetchone()
    print("You are connected to - ", record,"\n")

except (Exception, psycopg2.Error) as error :
    print ("Error while connecting to PostgreSQL", error)
finally:
    #closing database connection.
        if(connection):
            cursor.close()
            connection.close()
            print("PostgreSQL connection is closed")
Posted by: Guest on June-30-2020
0

Query a PSQL Database From Python

conn = psycopg2.connect("dbname=suppliers user=postgres password=postgres")Code language: SQL (Structured Query Language) (sql)
Posted by: Guest on July-14-2021

Code answers related to "accessing database with python postgresql"

Python Answers by Framework

Browse Popular Code Answers by Language