--- 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:
- **Connection String**
### Dynamically Configure Host and Database
ToolJet allows you to configure the Host and Database directly within the query instead of setting them in the data source configuration.
This is particularly useful in multi-tenant applications, where the same ToolJet application needs to connect to different databases based on the active tenant. Instead of creating multiple data sources for each tenant, you can define the host and database dynamically within the query.
To enable this feature, turn on the **Allow dynamic connection parameters** toggle on the data source configuration page.
Once you enable **Allow dynamic connection parameters**, you can write custom logic directly inside the query editor to determine which Host and Database to use based on the current logged-in user.
#### Example Logic
You can use the following code to dynamically configure the host based on the current user's email domain:
```js
{{(() => {
const domainMap = {
'tooljet.com': 'db1.internal.company.com',
'tenantA.com': 'db-tenant-a.company.com',
'tenantB.com': 'db-tenant-b.company.com',
'tenantC.com': 'db-tenant-c.company.com'
};
const email = globals.currentUser.email || '';
const domain = email.split('@')[1] || '';
return domainMap[domain] || 'default-db.company.com';
})()}}
```
**Note:** We recommend creating a new PostgreSQL database user to have control over ToolJet's access levels.
:::info
Please make sure the **Host/IP** of the database is accessible from your VPC if you have self-hosted ToolJet. If you are using ToolJet cloud, please **whitelist** our IP.
:::
### 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:
```json
[
{
"customer_id": 1,
"country": "India"
},
{
"customer_id": 2,
"country": "USA"
}
]
```
:::tip
- You can apply transformations to the query results. Refer to our transformations documentation for more details: **[Transformation Tutorial](/docs/tutorial/transformations)**
- Check out this how-to guide on **[bulk updating multiple rows](/docs/how-to/bulk-update-multiple-rows)** from a table component.
:::