Saturday, December 28, 2024
Google search engine
HomeLanguagesPython PostgreSQL – Create Database

Python PostgreSQL – Create Database

In this article, we will discuss how to create database in PostgreSQL using pysopg2 in Python.

CREATE DATABASE is one of the Data Definition Language ( DDL ) statements supported by the PostgreSQL Database Management System. It is used to create database in PostgreSQL. Database name should be always unique. If it already exists then it shows that the particular database already exists. 

Syntax: CREATE DATABASE database_name;

Example: Creating Database using Pyscopg2 

Python3




import psycopg2
 
# connection establishment
conn = psycopg2.connect(
   database="postgres",
    user='postgres',
    password='password',
    host='localhost',
    port= '5432'
)
 
conn.autocommit = True
 
# Creating a cursor object
cursor = conn.cursor()
 
# query to create a database
sql = ''' CREATE database products ''';
 
# executing above query
cursor.execute(sql)
print("Database has been created successfully !!");
 
# Closing the connection
conn.close()


Output

Database has been created successfully !!

Let’s check the database in PostgreSQL:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments