> This page location: PostgreSQL Python > Insert Data Into Table in Python
> Full Neon documentation index: https://neon.com/docs/llms.txt

# PostgreSQL Python: Insert Data Into a Table

**Info:** Inserting rows from Python with psycopg2 works the same against any PostgreSQL database, so the patterns here carry over wherever you run Postgres. If you're an enterprise looking for managed Postgres built for the AI era, [Lakebase](https://www.databricks.com/product/lakebase) delivers high performance, strong security, and native integration with the Lakehouse. If you're a developer or startup who needs to ship and scale fast, [Neon](https://neon.com) gives you the best Postgres platform to build on.

**Summary**: in this tutorial, you will learn to insert one or more rows into a PostgreSQL table from Python.

This tutorial picks up from where the [Creating Tables Tutorial](https://neon.com/postgresql/python/create-tables) left off.

## Steps for inserting one row into a table from Python

To insert one or more rows into a table from Python, you follow these steps:

- First, [connect to the PostgreSQL server](https://neon.com/postgresql/python/connect).
- Next, create a `cursor` object from the `connection` object.
- Then, execute the [INSERT](../postgresql-tutorial/postgresql-insert) statement with values by calling the `execute()` or `executemany()` method of the `cursor` object.
- After that, commit the transaction by calling the `commit()` method of the `connection` object.
- Finally, obtain the inserted ID by calling the `fetchone()` method of the `cursor` object.

## Inserting data into a table example

We will use the `vendors` table in the `suppliers` database for the demonstration purposes:

![vendors\_table](https://neon.com/postgresqltutorial/vendors_table.png)

### 1) Inserting one row into a table

First, create a new file named insert.py.

Second, define a `insert_vendor()` function in the insert.py file:

```python
import psycopg2
from config import load_config


def insert_vendor(vendor_name):
    """ Insert a new vendor into the vendors table """

    sql = """INSERT INTO vendors(vendor_name)
             VALUES(%s) RETURNING vendor_id;"""

    vendor_id = None
    config = load_config()

    try:
        with  psycopg2.connect(**config) as conn:
            with  conn.cursor() as cur:
                # execute the INSERT statement
                cur.execute(sql, (vendor_name,))

                # get the generated id back
                rows = cur.fetchone()
                if rows:
                    vendor_id = rows[0]

                # commit the changes to the database
                conn.commit()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
    finally:
        return vendor_id


if __name__ == '__main__':
    insert_vendor("3M Co.")
```

The `insert_vendor()` function will insert a new row into the `vendors` table and return the inserted `vendor_id`.

### 2) Inserting multiple rows into a table

The steps for inserting multiple rows into a table are similar to the steps for inserting one row.

The key difference is in the third step: instead of calling the `execute()` method of the `cursor` object, you use the `executemany()` method.

For example, the following defines `insert_many_vendors()` function that inserts multiple rows into the `vendors` table:

```python
def insert_many_vendors(vendor_list):
    """ Insert multiple vendors into the vendors table  """

    sql = "INSERT INTO vendors(vendor_name) VALUES(%s) RETURNING *"
    config = load_config()
    try:
        with  psycopg2.connect(**config) as conn:
            with  conn.cursor() as cur:
                # execute the INSERT statement
                cur.executemany(sql, vendor_list)

            # commit the changes to the database
            conn.commit()
    except (Exception, psycopg2.DatabaseError) as error:
        print(error)
```

The following uses the `insert_vendor()` and `insert_many_vendors()` functions to insert one and multiple rows into the `vendors` table:

```python
if __name__ == '__main__':
    insert_vendor("3M Co.")

    insert_many_vendors([
        ('AKM Semiconductor Inc.',),
        ('Asahi Glass Co Ltd.',),
        ('Daikin Industries Ltd.',),
        ('Dynacast International Inc.',),
        ('Foster Electric Co. Ltd.',),
        ('Murata Manufacturing Co. Ltd.',)
    ])
```

### 3) Execute the insert.py module

First, open the Command Prompt on Windows or Terminal on Unix-like systems.

Second, run the following command to execute the `insert.py` module:

```bash
python insert.py
```

### 4) Verify the inserts

First, [connect to the PostgreSQL server](https://neon.com/postgresql/python/connect) using the `psql`:

```bash
psql -U postgres
```

Second, change the current database to `suppliers`:

```
\c suppliers
```

Third, retrieve data from the `vendors` table:

```sql
SELECT * FROM vendors;
```

Output:

```
 vendor_id |          vendor_name
-----------+-------------------------------
         1 | 3M Co.
         2 | AKM Semiconductor Inc.
         3 | Asahi Glass Co Ltd.
         4 | Daikin Industries Ltd.
         5 | Dynacast International Inc.
         6 | Foster Electric Co. Ltd.
         7 | Murata Manufacturing Co. Ltd.
(7 rows)
```

[Download the project source code](https://neon.com/postgresqltutorial/insert.zip)

## Summary

- Use the `execute()` or `executemany()` method of the `cursor` object to insert one or more rows into a table from Python.

---

## Related docs (PostgreSQL Python)

- [Connect To PostgreSQL Database](https://neon.com/postgresql/python/connect)
- [Create Tables in Python](https://neon.com/postgresql/python/create-tables)
- [Update Data in Python](https://neon.com/postgresql/python/update)
- [Query Data in Python](https://neon.com/postgresql/python/query)
- [Handle Transactions in Python](https://neon.com/postgresql/python/transaction)
- [Call PostgreSQL Functions in Python](https://neon.com/postgresql/python/python-call-postgresql-functions)
- [Call PostgreSQL Stored Procedures in Python](https://neon.com/postgresql/python/call-stored-procedures)
- [Work with BLOB Data in Python](https://neon.com/postgresql/python/blob)
- [Delete Data from Tables in Python](https://neon.com/postgresql/python/delete)
