- Python Data Access - Discussion
- Python Data Access - Useful Resources
- Python Data Access - Quick Guide
- Python MongoDB - Limit
- Python MongoDB - Update
- Python MongoDB - Drop Collection
- Python MongoDB - Delete Document
- Python MongoDB - Sort
- Python MongoDB - Query
- Python MongoDB - Find
- Python MongoDB - Insert Document
- Python MongoDB - Create Collection
- Python MongoDB - Create Database
- Python MongoDB - Introduction
- Python SQLite - Cursor Object
- Python SQLite - Join
- Python SQLite - Limit
- Python SQLite - Drop Table
- Python SQLite - Delete Data
- Python SQLite - Update Table
- Python SQLite - Order By
- Python SQLite - Where Clause
- Python SQLite - Select Data
- Python SQLite - Insert Data
- Python SQLite - Create Table
- Python SQLite - Establishing Connection
- Python SQLite - Introduction
- Python PostgreSQL - Cursor Object
- Python PostgreSQL - Join
- Python PostgreSQL - Limit
- Python PostgreSQL - Drop Table
- Python PostgreSQL - Delete Data
- Python PostgreSQL - Update Table
- Python PostgreSQL - Order By
- Python PostgreSQL - Where Clause
- Python PostgreSQL - Select Data
- Python PostgreSQL - Insert Data
- Python PostgreSQL - Create Table
- Python PostgreSQL - Create Database
- Python PostgreSQL - Database Connection
- Python PostgreSQL - Introduction
- Python MySQL - Cursor Object
- Python MySQL - Join
- Python MySQL - Limit
- Python MySQL - Drop Table
- Python MySQL - Delete Data
- Python MySQL - Update Table
- Python MySQL - Order By
- Python MySQL - Where Clause
- Python MySQL - Select Data
- Python MySQL - Insert Data
- Python MySQL - Create Table
- Python MySQL - Create Database
- Python MySQL - Database Connection
- Python MySQL - Introduction
- Python Data Access - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python PostgreSQL - Create Database
You can create a database in PostgreSQL using the CREATE DATABASE statement. You can execute this statement in PostgreSQL shell prompt by specifying the name of the database to be created after the command.
Syntax
Following is the syntax of the CREATE DATABASE statement.
CREATE DATABASE dbname;
Example
Following statement creates a database named testdb in PostgreSQL.
postgres=# CREATE DATABASE testdb; CREATE DATABASE
You can pst out the database in PostgreSQL using the l command. If you verify the pst of databases, you can find the newly created database as follows −
postgres=# l List of databases Name | Owner | Encoding | Collate | Ctype | -----------+----------+----------+----------------------------+-------------+ mydb | postgres | UTF8 | Engpsh_United States.1252 | ........... | postgres | postgres | UTF8 | Engpsh_United States.1252 | ........... | template0 | postgres | UTF8 | Engpsh_United States.1252 | ........... | template1 | postgres | UTF8 | Engpsh_United States.1252 | ........... | testdb | postgres | UTF8 | Engpsh_United States.1252 | ........... | (5 rows)
You can also create a database in PostgreSQL from command prompt using the command createdb, a wrapper around the SQL statement CREATE DATABASE.
C:Program FilesPostgreSQL11in> createdb -h localhost -p 5432 -U postgres sampledb Password:
Creating a database using python
The cursor class of psycopg2 provides various methods execute various PostgreSQL commands, fetch records and copy data. You can create a cursor object using the cursor() method of the Connection class.
The execute() method of this class accepts a PostgreSQL query as a parameter and executes it.
Therefore, to create a database in PostgreSQL, execute the CREATE DATABASE query using this method.
Example
Following python example creates a database named mydb in PostgreSQL database.
import psycopg2 #estabpshing the connection conn = psycopg2.connect( database="postgres", user= postgres , password= password , host= 127.0.0.1 , port= 5432 ) conn.autocommit = True #Creating a cursor object using the cursor() method cursor = conn.cursor() #Preparing query to create a database sql = CREATE database mydb ; #Creating a database cursor.execute(sql) print("Database created successfully........") #Closing the connection conn.close()
Output
Database created successfully........Advertisements