|
1 | | - |
2 | 1 | # Connect PostgreSQL database with Python |
3 | 2 |
|
4 | 3 | In this section, I'll explain how to connect your PostgreSQL database to Python and query it. |
5 | 4 |
|
6 | | - |
7 | 5 | ## Install "Psycopg2" |
| 6 | + |
8 | 7 | To access the PostgreSQL database, Python needs a PostgreSQL driver. You must first install the `Psycopg2` package on your computer. |
| 8 | + |
9 | 9 | ```bash |
10 | | -python -m pip install psycopg2 |
| 10 | +python -m pip install psycopg2 |
11 | 11 | ``` |
12 | 12 |
|
13 | 13 | ## Import Psycopg2" |
| 14 | + |
14 | 15 | To access the PostgreSQL database, import `psycopg2` library into your Python code. |
| 16 | + |
15 | 17 | ```python |
16 | | -import psycopg2 |
| 18 | +import psycopg2 |
17 | 19 | ``` |
18 | 20 |
|
19 | 21 | ## Creating a Database: |
| 22 | + |
20 | 23 | For the purpose of example, we will need a sample database. To do so, follow the below steps: |
| 24 | + |
21 | 25 | - First, open a PostgreSQL client tool like `pgadmin4` or `psql`. |
22 | 26 | - Second login to the database using your credentials. |
23 | 27 | - Finally, run the following command to create a database (for example, `company`). |
| 28 | + |
24 | 29 | ```sql |
25 | | -CREATE DATABASE company; |
| 30 | +CREATE DATABASE company; |
26 | 31 | ``` |
27 | 32 |
|
28 | 33 | ## Create Connection: |
| 34 | + |
29 | 35 | To connect to the above created database (i.e., `company`), we use the `connect()` function. |
30 | 36 | So, establish a connection to the database. Use your PostgreSQL database's username and password in the Python code. |
| 37 | + |
31 | 38 | ```python |
32 | | -mydb = psycopg2.connect( |
33 | | - host="localhost", |
34 | | - user="yourUsername", |
35 | | - password="yourPassword", |
36 | | - database="company" |
37 | | -) |
| 39 | +mydb = psycopg2.connect( |
| 40 | + host="localhost", |
| 41 | + user="yourUsername", |
| 42 | + password="yourPassword", |
| 43 | + database="company" |
| 44 | +) |
38 | 45 | ``` |
39 | 46 |
|
40 | 47 | ## Make Cursor: |
| 48 | + |
41 | 49 | You must create or include a `cursor()` in your Python code in order to run SQL queries. |
| 50 | + |
42 | 51 | ```python |
43 | | -mycursor = mydb.cursor() |
| 52 | +mycursor = mydb.cursor() |
44 | 53 | ``` |
| 54 | + |
45 | 55 | Now you can use SQL commands to query the database. |
46 | 56 |
|
47 | 57 | ## Commit Changes: |
| 58 | + |
48 | 59 | You also have to include the commit function and set automatic commit to be `true`. |
| 60 | + |
49 | 61 | ```python |
50 | | -mydb.set_session(autocommit=True) |
| 62 | +mydb.set_session(autocommit=True) |
51 | 63 | ``` |
| 64 | + |
52 | 65 | So that each action is committed or saved without having to call `mydb.commit()` after each command. |
53 | 66 |
|
54 | 67 | ## Query the Database: |
| 68 | + |
55 | 69 | Now create a table in the database using the cursor we created. |
| 70 | + |
56 | 71 | ```python |
57 | | -mycursor.execute('''CREATE TABLE employee( |
58 | | - EmployeeID int, |
59 | | - Name varchar(255), |
60 | | - Email varchar(255)); |
61 | | -''') |
| 72 | +mycursor.execute('''CREATE TABLE employee( |
| 73 | + EmployeeID int, |
| 74 | + Name varchar(255), |
| 75 | + Email varchar(255)); |
| 76 | +''') |
62 | 77 | ``` |
63 | 78 |
|
64 | 79 | ## Insert Data Into Table: |
| 80 | + |
65 | 81 | Now we are inserting the values into the database. |
| 82 | + |
66 | 83 | ```python |
67 | | -mycursor.execute(''' |
68 | | -INSERT INTO employee (EmployeeID, Name, Email) |
69 | | - VALUES (101, 'Mark', 'mark@company.com'), |
70 | | - (102, 'Robert', 'robert@company.com'), |
71 | | - (103, 'Spencer', 'spencer@company.com'); |
72 | | -''') |
| 84 | +mycursor.execute(''' |
| 85 | +INSERT INTO employee (EmployeeID, Name, Email) |
| 86 | + VALUES (101, 'Mark', 'mark@company.com'), |
| 87 | + (102, 'Robert', 'robert@company.com'), |
| 88 | + (103, 'Spencer', 'spencer@company.com'); |
| 89 | +''') |
73 | 90 | ``` |
74 | 91 |
|
75 | 92 | ## Retrive the Data: |
| 93 | + |
76 | 94 | Now let’s query the database. |
| 95 | + |
77 | 96 | ```python |
78 | | -mycursor.execute("SELECT * FROM employee") |
| 97 | +mycursor.execute("SELECT * FROM employee") |
79 | 98 | ``` |
| 99 | + |
80 | 100 | It’s important to note that after the query executes, you still need to use one of the `psycopg2` functions to retrieve data rows. |
81 | 101 |
|
82 | 102 | - fetchone() |
83 | 103 | - fetchall() |
84 | 104 | - fetchmany() |
85 | 105 |
|
86 | 106 | For example, |
| 107 | + |
87 | 108 | ```python |
88 | | -print(mycursor.fetchone()) |
| 109 | +print(mycursor.fetchone()) |
89 | 110 | ``` |
90 | | -| EmployeeID | Name | Email | |
91 | | -| :--------- | :------- | :---------------- | |
92 | | -| 101 | Mark | mark@company.com | |
93 | 111 |
|
94 | | -The most basic way to fetch data from your database is to use the `fetchone()` function. This function will return exactly one row (the first row) after executing the SQL query. |
| 112 | +| EmployeeID | Name | Email | |
| 113 | +| :--------- | :--- | :--------------- | |
| 114 | +| 101 | Mark | mark@company.com | |
95 | 115 |
|
| 116 | +The most basic way to fetch data from your database is to use the `fetchone()` function. This function will return exactly one row (the first row) after executing the SQL query. |
96 | 117 |
|
97 | 118 | ```python |
98 | | -print(mycursor.fetchall()) |
| 119 | +print(mycursor.fetchall()) |
99 | 120 | ``` |
100 | | -| EmployeeID | Name | Email | |
101 | | -| :--------- | :------- | :---------------- | |
102 | | -| 101 | Mark | mark@company.com | |
103 | | -| 102 | Robert | robert@company.com | |
104 | | -| 103 | Spencer | spencer@company.com | |
| 121 | + |
| 122 | +| EmployeeID | Name | Email | |
| 123 | +| :--------- | :------ | :------------------ | |
| 124 | +| 101 | Mark | mark@company.com | |
| 125 | +| 102 | Robert | robert@company.com | |
| 126 | +| 103 | Spencer | spencer@company.com | |
105 | 127 |
|
106 | 128 | If you need more than one row from your database, you can use `fetchall()`, which works the same as `fetchone()` except that it returns all the rows. |
107 | 129 |
|
108 | 130 | ```python |
109 | | -print(mycursor.fetchmany(2)) |
| 131 | +print(mycursor.fetchmany(2)) |
110 | 132 | ``` |
111 | | -| EmployeeID | Name | Email | |
112 | | -| :--------- | :------- | :---------------- | |
113 | | -| 101 | Mark | mark@company.com | |
114 | | -| 102 | Robert | robert@company.com | |
| 133 | + |
| 134 | +| EmployeeID | Name | Email | |
| 135 | +| :--------- | :----- | :----------------- | |
| 136 | +| 101 | Mark | mark@company.com | |
| 137 | +| 102 | Robert | robert@company.com | |
115 | 138 |
|
116 | 139 | With `fetchmany()`, you have another option to retrieve multiple records from the database and have more control over the exact number of rows retrieved. |
117 | 140 |
|
118 | | -You can use any of them depending on your needs. |
| 141 | +You can use any of them depending on your needs. |
119 | 142 |
|
120 | 143 | ## Close the Connection: |
| 144 | + |
121 | 145 | In the end, you must close the cursor and the connection. |
| 146 | + |
122 | 147 | ```python |
123 | | -mycursor.close() |
124 | | -mydb.close() |
| 148 | +mycursor.close() |
| 149 | +mydb.close() |
125 | 150 | ``` |
126 | 151 |
|
127 | 152 | You can also get code snippet from [here](https://github.com/kishlayjeet/Connect-PostgreSQL-database-to-Python/blob/d7c5cae0a809ec0714cf193c5db6a77f30e70502/code-snippet.py). |
128 | 153 |
|
129 | 154 | ## Authors |
| 155 | + |
130 | 156 | I am [Kishlay](https://www.github.com/kishlayjeet), and I have written this tutorial, but other people have also helped me with it. |
131 | 157 | If you have any trouble with this tutorial, please tell me about it, and I will make it better. |
132 | 158 | If you like this tutorial and this helped you out, then please give it a star. |
133 | 159 |
|
134 | 160 | ## Feedback |
135 | | -If you have any feedback, please reach out to me at contact.kishlayjeet@gmail.com |
136 | | - |
137 | 161 |
|
| 162 | +If you have any feedback, please reach out to me at contact.kishlayjeet@gmail.com |
0 commit comments