World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Download our e-book of Introduction To Python
4.5 (1,292 Ratings)
559 Learners
Neha Kumawat
a year ago
C:\Users\Tutorialspoint>python -m pip install --upgrade pip
Collecting pip
Using cached
https://files.pythonhosted.org/packages/8d/07/f7d7ced2f97ca3098c16565efbe6b15fafcba53e8d9bdb431e09140514b0/pip-19.2.2-py2.py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 19.0.3
Uninstalling pip-19.0.3:
Successfully uninstalled pip-19.0.3
Successfully installed pip-19.2.2
C:\WINDOWS\system32>pip install psycopg2-binary
Collecting psycopg2-binary
Using cached
https://files.pythonhosted.org/packages/80/79/d0d13ce4c2f1addf4786f4a2ded802c2df66ddf3c1b1a982ed8d4cb9fc6d/psycopg2_binary-2.8.3-cp37-cp37m-win32.whl
Installing collected packages: psycopg2-binary
Successfully installed psycopg2-binary-2.8.3
import mysql.connector
D:\Python_PostgreSQL>import psycopg2
D:\Python_PostgreSQL>
import psycopg2
#establishing the connection
conn = psycopg2.connect(
database="postgres", user='postgres', password='password',
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()
Connection established to: (
'PostgreSQL 11.5, compiled by Visual C++ build 1914, 64-bit',
)
Connection established to: (
'PostgreSQL 11.5, compiled by Visual C++ build 1914, 64-bit',
)
CREATE DATABASE dbname;
postgres=# CREATE DATABASE testdb;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype |
-----------+----------+----------+----------------------------+-------------+
mydb | postgres | UTF8 | English_United States.1252 | ........... |
postgres | postgres | UTF8 | English_United States.1252 | ........... |
template0 | postgres | UTF8 | English_United States.1252 | ........... |
template1 | postgres | UTF8 | English_United States.1252 | ........... |
testdb | postgres | UTF8 | English_United States.1252 | ........... |
(5 rows)
C:\Program Files\PostgreSQL\11\bin> createdb -h localhost -p 5432 -U postgres sampledb
Password:
import psycopg2
#establishing 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()
Database created successfully........
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
);
postgres=# CREATE TABLE CRICKETERS (
First_Name VARCHAR(255),
Last_Name VARCHAR(255),
Age INT,
Place_Of_Birth VARCHAR(255),
Country VARCHAR(255));
CREATE TABLE
postgres=#
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+------------+-------+----------
public | cricketers | table | postgres
(1 row)
postgres=#
postgres=# \d cricketers
Table "public.cricketers"
Column | Type | Collation | Nullable | Default
----------------+------------------------+-----------+----------+---------
first_name | character varying(255) | | |
last_name | character varying(255) | | |
age | integer | | |
place_of_birth | character varying(255) | | |
country | character varying(255) | | |
postgres=#
import psycopg2
#Establishing the connection
conn = psycopg2.connect(
database="mydb", user='postgres', password='password', host='127.0.0.1', port= '5432'
)
#Creating a cursor object using the cursor() method
cursor = conn.cursor()
#Doping EMPLOYEE table if already exists.
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
#Creating table as per requirement
sql ='''CREATE TABLE EMPLOYEE(
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT)'''
cursor.execute(sql)
print("Table created successfully........")
#Closing the connection
conn.close()
Table created successfully........
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
postgres=# CREATE TABLE CRICKETERS (
First_Name VARCHAR(255),
Last_Name VARCHAR(255),
Age INT,
Place_Of_Birth VARCHAR(255),
Country VARCHAR(255)
);
CREATE TABLE
postgres=#
postgres=# insert into CRICKETERS
(First_Name, Last_Name, Age, Place_Of_Birth, Country) values
('Shikhar', 'Dhawan', 33, 'Delhi', 'India');
INSERT 0 1
postgres=#
postgres=# insert into CRICKETERS
(First_Name, Last_Name, Country) values('Jonathan', 'Trott', 'SouthAfrica');
INSERT 0 1
postgres=# insert into CRICKETERS values('Kumara', 'Sangakkara', 41, 'Matale', 'Srilanka');
INSERT 0 1
postgres=# insert into CRICKETERS values('Virat', 'Kohli', 30, 'Delhi', 'India');
INSERT 0 1
postgres=# insert into CRICKETERS values('Rohit', 'Sharma', 32, 'Nagpur', 'India');
INSERT 0 1
postgres=#
postgres=# SELECT * from CRICKETERS;
first_name | last_name | age | place_of_birth | country
------------+------------+-----+----------------+-------------
Shikhar | Dhawan | 33 | Delhi | India
Jonathan | Trott | | | SouthAfrica
Kumara | Sangakkara | 41 | Matale | Srilanka
Virat | Kohli | 30 | Delhi | India
Rohit | Sharma | 32 | Nagpur | India
(5 rows)