[id='configuring-sql-cache-stores-table_{context}']
= Loading {brandname} caches from database tables

Add a SQL table cache store to your configuration if you want {brandname} to load data from a database table.
When it connects to the database, {brandname} uses metadata from the table to detect column names and data types.
{brandname} also automatically determines which columns in the database are part of the primary key.

.Prerequisites

* Have JDBC connection details. +
You can add JDBC connection factories directly to your cache configuration. +
For remote caches in production environments, you should add managed datasources to {brandname} Server configuration and specify the JNDI name in the cache configuration.
* Remote caches: Copy database drivers to the `server/lib` directory in your {brandname} Server installation.
+
[TIP]
====
Use the [command]`install` command with the {brandname} Command Line Interface (CLI) to download the required drivers to the `server/lib` directory, for example:

[source,bash,options="nowrap",subs=attributes+]
----
install org.postgresql:postgresql:42.1.3
----
====
* Embedded caches: Add the `infinispan-cachestore-sql` dependency to your `pom` file.
+
[source,xml,options="nowrap",subs=attributes+]
----
include::dependencies_maven/persistence_sql_store.xml[]
----

.Procedure

. Create a cache store configuration that loads from a single database table.
+
.Declarative
[source,xml,options="nowrap",subs=attributes+,role="primary"]
----
table-jdbc-store xmlns="urn:infinispan:config:store:sql:{schemaversion}"
----
+
.Programmatic
[source,xml,options="nowrap",subs=attributes+,role="secondary"]
----
persistence().addStore(TableJdbcStoreConfigurationBuilder.class)
----

. Specify the database dialect with either `dialect=""` or `dialect()`, for example `dialect="H2"` or `dialect="postgres"`.
. Configure the SQL cache store with the properties you require, for example:
+
* To use the same cache store across your cluster, set `shared="true"` or `shared(true)`.
+
* To create a read only cache store, set `read-only="true"` or `.ignoreModifications(true)`.
. Name the database table that loads the cache with `table-name="<database_table_name>"` or `table.name("<database_table_name>")`.
. Add the `schema` element or the `.schemaJdbcConfigurationBuilder()` method and provide your Protobuf schema details if your database tables contain composite keys or values.

[role="_additional-resources"]
.Additional resources
* link:{config_docs}#jdbc-connection-factories_persistence[JDBC connection factories]
* link:{javadocroot}/org/infinispan/persistence/jdbc/common/DatabaseType.html[DatabaseType Enum lists supported database dialects]
* link:{configdocroot}infinispan-cachestore-sql-config-{schemaversion}.html[{brandname} SQL store configuration reference]
