--- id: postgresql title: PostgreSQL --- ToolJet has the capability to connect to PostgreSQL databases for data retrieval and modification.
### Connection String
To connect to PostgreSQL using a connection string, select **Connection String** as the connection type and provide the following details:
:::info
If you connection string (username,password, database) has any special characters, then you have to URL Encode them.
const POSTGRES_URL = `postgresql://${encodeURIComponent(user)}:${encodeURIComponent(password)}@${host}:${port}/${encodeURIComponent(database)}`;
:::
## Querying PostgreSQL
1. Click on **+ Add** button of the query manager at the bottom panel of the editor and select the database added in the previous step as the data source.
2. Select the operation that you want to perform and click **Save** to save the query.
3. Click on the **Run** button to run the query.
## Querying in SQL Mode
1. Create a new query and select the PostgreSQL data source.
2. Select the SQL query mode from the dropdown and enter the query.
3. Click on the **Preview** button to preview the output or Click on the **Run** button to trigger the query.
### Parameterized Queries
ToolJet offers support for parameterized SQL queries, which enhance security by preventing SQL injection and allow for dynamic query construction. To implement parameterized queries:
1. Use `:parameter_name` as placeholders in your SQL query where you want to insert parameters.
2. In the **Parameters** section below the query editor, add key-value pairs for each parameter.
3. The keys should match the parameter names used in the query (without the colon).
4. The values can be static values or dynamic values using the `{{ }}` notation.
#### Example:
```yaml
Query: SELECT * FROM users WHERE username = :username
```
SQL Parameters:
## Querying in GUI Mode
1. Create a new query and select the PostgreSQL data source.
2. Select the GUI mode from the dropdown.
3. Select the operation you want to perform.
4. Select the **Schema**, **Table** and add the **Primary key column** name.
5. Click on the **Preview** button to preview the output or Click on the **Run** button to trigger the query.
### List Rows
Retrieves and displays rows from the selected table based on optional filters, sorting, and limits.
#### Optional Parameters
- **Filter**: Applies conditions to return only rows that match the specified criteria.
- **Sort**: Orders the returned rows based on one or more selected columns.
- **Aggregate**: Performs calculations such as count, sum, or average on selected columns.
- **Group by**: Groups rows that have the same values in specified columns to enable aggregation.
- **Limit**: Restricts the number of rows returned in the result.
- **Offset**: Skips a specified number of rows before starting to return results.
### Create Rows
Inserts a new row into the selected table with the specified column values.
In the editor, ensure to the input the **Columns** in `string` format.
#### Required Parameters
- **Columns**: Specifies the table columns and their corresponding values to be inserted when creating a new row.
### Update Rows
Modifies existing rows in the table that match the provided filter conditions.
In the editor, ensure to the input the **Columns** in `string` format.
#### Required Parameters
- **Columns**: Specify the columns and their new values that should be updated for the matching rows.
#### Optional Parameters
- **Filter**: Defines conditions to select which rows should be updated.
### Delete Rows
Removes either all rows from the table or that match the specified filter conditions.
#### Optional Parameters
- **Filter**: Specifies conditions to determine which rows should be deleted from the table.
- **Limit**: Restricts the maximum number of rows that can be deleted in the operation.
### Upsert Rows
Inserts a new row or updates an existing row if a matching primary key already exists. In the editor, ensure to the input the **Columns** in `string` format.
#### Required Parameters
- **Primary Key column(s)**: Specifies the column(s) used to identify whether a row already exists for updating or if a new row should be inserted.
- **Columns**: Defines the column–value pairs that will be inserted or updated in the row.
### Bulk Insert
Inserts multiple rows into the table in a single operation using an array of records.
#### Required Parameters
- **Records to Insert**: An array of objects representing multiple rows to be inserted into the selected table in a single operation.
### Bulk Update using Primary Key
Updates multiple rows at once by matching each record with its corresponding primary key.
#### Required Parameters
- **Primary Key columns**: Specifies the column(s) used to uniquely identify the rows that should be updated.
- **Records to Update**: An array of objects containing the primary key and the column values to be updated for each row.
### Bulk Upsert using Primary Key
Inserts new rows or updates existing rows in bulk based on matching primary key values.
#### Required Parameters
- **Primary Key columns**: Specifies the column(s) used to determine whether a row already exists for updating or if a new row should be inserted.
- **Records to Update**: An array of objects containing primary key values and column data that will be inserted as new rows or used to update existing rows.
This basically means If the row exists then update, if not do insert.
:::tip
- You can apply transformations to the query results. Refer to our transformations documentation for more details: **[Transformation Tutorial](/docs/app-builder/custom-code/transform-data)**
- Check out this how-to guide on **[bulk updating multiple rows](/docs/how-to/bulk-update-multiple-rows)** from a table component.
:::