PostgreSQL
This component allows using PostgreSQL (Postgres) as state store for Dapr.
Create a file called , paste the following and replace the <CONNECTION STRING>
value with your connection string. The connection string is a standard PostgreSQL connection string. For example, "host=localhost user=postgres password=example port=5432 connect_timeout=10 database=dapr_test"
. See the PostgreSQL documentation on database connections for information on how to define a connection string.
If you want to also configure PostgreSQL to store actors, add the actorStateStore
option as in the example below.
Warning
Run an instance of PostgreSQL. You can run a local instance of PostgreSQL in Docker CE with the following command:
This example does not describe a production configuration because it sets the password in plain text and the user name is left as the PostgreSQL default of “postgres”.
This state store supports for records stored with Dapr. When storing data using Dapr, you can set the ttlInSeconds
metadata property to indicate after how many seconds the data should be considered “expired”.
Because PostgreSQL doesn’t have built-in support for TTLs, this is implemented in Dapr by adding a column in the state table indicating when the data is to be considered “expired”. Records that are “expired” are not returned to the caller, even if they’re still physically stored in the database. A background “garbage collector” periodically scans the state table for expired rows and deletes them.
The interval at which the deletion of expired records happens is set with the cleanupIntervalInSeconds
metadata property, which defaults to 3600 seconds (that is, 1 hour).
- Longer intervals require less frequent scans for expired rows, but can require storing expired records for longer, potentially requiring more storage space. If you plan to store many records in your state table, with short TTLs, consider setting
cleanupIntervalInSeconds
to a smaller value, for example300
(300 seconds, or 5 minutes). - If you do not plan to use TTLs with Dapr and the PostgreSQL state store, you should consider setting
cleanupIntervalInSeconds
to a value <= 0 (e.g.0
or-1
) to disable the periodic cleanup and reduce the load on the database.
- Read this guide for instructions on configuring state store components