Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b0cae8

Browse files
refine it
1 parent 0968c2b commit 1b0cae8

File tree

1 file changed

+57
-42
lines changed

1 file changed

+57
-42
lines changed

‎README.md‎

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
1-
# Connect PostgreSQL database with Python
1+
# Connect PostgreSQL Database with Python
22

3-
In this section, I'll explain how to connect your PostgreSQL database to Python and query it.
3+
This tutorial explains how to connect your PostgreSQL database to Python and query it.
44

5-
## Install "Psycopg2"
5+
## Prerequisites
66

7-
To access the PostgreSQL database, Python needs a PostgreSQL driver. You must first install the `Psycopg2` package on your computer.
7+
Before you begin, make sure you have the following installed:
8+
9+
- Python: You can download and install Python from the official website (https://www.python.org).
10+
- PostgreSQL: Install PostgreSQL on your computer by following the instructions from the official website (https://www.postgresql.org).
11+
12+
## Install the "psycopg2" Package
13+
14+
To access the PostgreSQL database, Python needs a PostgreSQL driver. You can install the `psycopg2` package using pip, the Python package installer. Open your command prompt or terminal and run the following command:
815

916
```bash
10-
python -m pip install psycopg2
17+
pip install psycopg2
1118
```
1219

13-
## Import Psycopg2"
20+
## Import the "psycopg2" Library
1421

15-
To access the PostgreSQL database, import `psycopg2` library into your Python code.
22+
To use the `psycopg2` package in your Python code, import the `psycopg2` module:
1623

1724
```python
1825
import psycopg2
1926
```
2027

21-
## Creating a Database:
28+
## Creating a Database
2229

23-
For the purpose of example, we will need a sample database. To do so, follow the below steps:
30+
For the purpose of this example, we will need a sample database. Follow the steps below to create it:
2431

25-
- First, open a PostgreSQL client tool like `pgadmin4` or `psql`.
26-
- Second login to the database using your credentials.
27-
- Finally, run the following command to create a database (for example, `company`).
32+
1. Open a PostgreSQL client tool like `pgadmin4` or `psql`.
33+
2. Log in to the database using your credentials.
34+
3. Run the following command to create a database, for example, `company`:
2835

2936
```sql
3037
CREATE DATABASE company;
3138
```
3239

33-
## Create Connection:
40+
## Create Connection
3441

35-
To connect to the above created database (i.e., `company`), we use the `connect()` function.
36-
So, establish a connection to the database. Use your PostgreSQL database's username and password in the Python code.
42+
To connect to the previously created database (`company`), we use the `connect()` function. Establish a connection to the database by providing your PostgreSQL database's username, password, and database name in the Python code:
3743

3844
```python
3945
mydb = psycopg2.connect(
@@ -44,29 +50,29 @@ mydb = psycopg2.connect(
4450
)
4551
```
4652

47-
## Make Cursor:
53+
## Create a Cursor
4854

49-
You must create or include a `cursor()` in your Python code in order to run SQL queries.
55+
You need to create a cursor to execute SQL queries in your Python code:
5056

5157
```python
5258
mycursor = mydb.cursor()
5359
```
5460

5561
Now you can use SQL commands to query the database.
5662

57-
## Commit Changes:
63+
## Commit Changes
5864

59-
You also have to include the commit function and set automatic commit to be `true`.
65+
You also need to include the commit function and set the automatic commit to be `True`:
6066

6167
```python
6268
mydb.set_session(autocommit=True)
6369
```
6470

65-
So that each action is committed or saved without having to call `mydb.commit()` after each command.
71+
This ensures that each action is committed or saved without having to call `mydb.commit()` after each command.
6672

67-
## Query the Database:
73+
## Query the Database
6874

69-
Nowcreate a table in the database using the cursor we created.
75+
Now, let's create a table in the database using the cursor we created:
7076

7177
```python
7278
mycursor.execute('''CREATE TABLE employee(
@@ -76,9 +82,9 @@ mycursor.execute('''CREATE TABLE employee(
7682
''')
7783
```
7884

79-
## Insert Data Into Table:
85+
## Insert Data Into Table
8086

81-
Now we are inserting the values into the database.
87+
Now, let's insert values into the database:
8288

8389
```python
8490
mycursor.execute('''
@@ -89,74 +95,83 @@ INSERT INTO employee (EmployeeID, Name, Email)
8995
''')
9096
```
9197

92-
## Retrive the Data:
98+
## Retrieve the Data
9399

94-
Now let’s query the database.
100+
Let's query the database:
95101

96102
```python
97103
mycursor.execute("SELECT * FROM employee")
98104
```
99105

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.
106+
After executing the query, you can use one of the `psycopg2` functions to retrieve data rows:
101107

102-
- fetchone()
103-
- fetchall()
104-
- fetchmany()
108+
- `fetchone()`: Retrieves exactly one row (the first row) after executing the SQL query.
109+
- `fetchall()`: Retrieves all the rows.
110+
- `fetchmany()`: Retrieves a specific number of rows.
105111

106-
For example,
112+
For example:
107113

108114
```python
109115
print(mycursor.fetchone())
110116
```
111117

118+
Output:
119+
112120
| EmployeeID | Name | Email |
113121
| :--------- | :--- | :--------------- |
114122
| 101 | Mark | mark@company.com |
115123

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.
124+
The most basic way to fetch data from your database is to use the `fetchone()` function. It returns exactly one row (the first row) after executing the SQL query.
117125

118126
```python
127+
128+
119129
print(mycursor.fetchall())
120130
```
121131

132+
Output:
133+
122134
| EmployeeID | Name | Email |
123135
| :--------- | :------ | :------------------ |
124136
| 101 | Mark | mark@company.com |
125137
| 102 | Robert | robert@company.com |
126138
| 103 | Spencer | spencer@company.com |
127139

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.
140+
If you need more than one row from your database, you can use `fetchall()`, which returns all the rows.
129141

130142
```python
131143
print(mycursor.fetchmany(2))
132144
```
133145

146+
Output:
147+
134148
| EmployeeID | Name | Email |
135149
| :--------- | :----- | :----------------- |
136150
| 101 | Mark | mark@company.com |
137151
| 102 | Robert | robert@company.com |
138152

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.
153+
With `fetchmany()`, you have another option to retrieve a specific number of rows from the database.
140154

141-
You can use any of them depending on your needs.
155+
Choose the appropriate function based on your needs.
142156

143-
## Close the Connection:
157+
## Close the Connection
144158

145-
In the end, you must close the cursor and the connection.
159+
Finally, remember to close the cursor and the connection:
146160

147161
```python
148162
mycursor.close()
149163
mydb.close()
150164
```
151165

152-
You can also get code snippet from [here](https://github.com/kishlayjeet/Connect-PostgreSQL-database-to-Python/blob/d7c5cae0a809ec0714cf193c5db6a77f30e70502/code-snippet.py).
166+
You can also find the code snippet [here](https://github.com/kishlayjeet/Connect-PostgreSQL-database-to-Python/blob/d7c5cae0a809ec0714cf193c5db6a77f30e70502/code-snippet.py).
153167

154168
## Authors
155169

156-
I am [Kishlay](https://www.github.com/kishlayjeet), and I have written this tutorial, but other people have also helped me with it.
157-
If you have any trouble with this tutorial, please tell me about it, and I will make it better.
158-
If you like this tutorial and this helped you out, then please give it a star.
170+
This tutorial was written by [Kishlay
171+
Jeet](https://www.github.com/kishlayjeet), with contributions from other people.
172+
If you encounter any issues with this tutorial, please let me know, and I will make improvements.
173+
If you found this tutorial helpful, please consider giving it a star.
159174

160175
## Feedback
161176

162-
If you have any feedback, please reach out to me at contact.kishlayjeet@gmail.com
177+
If you have any feedback, please reach out to me at contact.kishlayjeet@gmail.com.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /