Open In App

Multiple Postgres databases in psycopg2

PostgreSQL is the most powerful open-source object-relational database management system. Psycopg2 is the most popular PostgreSQL database adapter for Python language. It simply allows you to work with multiple databases in the same program at the same time. This indicates that you can easily switch between two different databases and can perform various operations on them, also efficiently managing your data too. With the use of psycopg2, you can write Python scripts to create, read, update, and delete data in Postgres databases, without using any external tools. It is a powerful and flexible tool that can be used to work with small as well as large-scale databases. It doesn’t matter if you are building a web application, managing a data warehouse, or creating a machine learning model, psycopg2 module can help you work with your Postgres databases as easily as possible.

Concepts related to the topic:



Required Modules:

pip install psycopg2

Implementation With Examples :

Steps Needed For Implementation :

Related Examples :

Example 1:

 Creating a new table in database1 and inserting data






import psycopg2
 
# Establishing connection with database1
conn1 = psycopg2.connect(
    database="database1",
    user="username",
    password="password",
    host="localhost",
    port="5432"
)
print("Connection successful!!")
 
# Creating a new table
cur1 = conn1.cursor()
cur1.execute('''CREATE TABLE employees
                (ID INT PRIMARY KEY NOT NULL,
                NAME TEXT NOT NULL,
                AGE INT NOT NULL,
                ADDRESS TEXT);''')
print("Table created successfully")
 
# Inserting data
cur1.execute(
    "INSERT INTO employees (ID, NAME, AGE, ADDRESS) VALUES (1, 'John', 30, 'California')")
conn1.commit()
print("Data inserted successfully")
 
# Closing connection
cur1.close()
conn1.close()
print("Connection Closed!!")

In this example, we establish a connection with the “database1” database using the psycopg2 library. Then, we create a new table called “employees” with the columns ID, NAME, AGE, and ADDRESS. We then insert data into this table. Finally, we close the connection with the “database1” database.

Function used :




conn1 = psycopg2.connect(
    database="database1",
    user="username",
    password="password",
    host="localhost",
    port="5432"
)




cur1 = conn1.cursor()




cur1.execute('''CREATE TABLE employees
                (ID INT PRIMARY KEY NOT NULL,
                NAME TEXT NOT NULL,
                AGE INT NOT NULL,
                ADDRESS TEXT);''')

This code can creates a new table with the given SQL queries named “employees” with four columns: ID (an integer primary key), NAME (text), AGE (integer), and ADDRESS (text).




conn1.commit()




cur1.close()
conn1.close()

Output :

Connection successful!!
Table created successfully
Data inserted successfully
Connection Closed!!

Example 2: 

Querying data from database2




import psycopg2
 
# Establishing connection with database2
conn2 = psycopg2.connect(
    database="database2",
    user="username",
    password="password",
    host="localhost",
    port="5432"
)
print("Successful Connection!!")
 
# Querying data
cur2 = conn2.cursor()
cur2.execute("SELECT * FROM employees")
rows = cur2.fetchall()
for row in rows:
    print(row)
 
# Closing connection
cur2.close()
conn2.close()
print("Connection closed!!!")

In this example, we establish a connection with the “database2” database using the psycopg2 library. We then query the “employees” table and print the retrieved data. Finally, we close the connection with the “database2” database.

Function used :

Here psycopg2.connect(), conn.cursor(), cur. close() and conn. close() are similar to example one 




cur2.execute("SELECT * FROM employees")




rows = cur2.fetchall()

Output :

Successful Connection!!
(1, 'John', 30, 'California')
Connection closed!!!

Article Tags :