How to set up Keycloak and Postgres using Neon
Tags: keycloak, neon, postgres, postgressql
Published: 2024-02-27 11:25:00

[Keycloak](https://www.keycloak.org/?utm_source=hackmamba&utm_medium=blog&utm_id=HMBcommunity) is a tool that helps manage user data by providing authentication and authorization services. It is highly secure, scalable, and flexible. Integrating Keycloak with [PostgreSQL](https://www.postgresql.org/?utm_source=hackmamba&utm_medium=blog&utm_id=HMBcommunity) ensures a stable, secure, and scalable foundation for your identity and access management system. However, setting up the database integration can be challenging due to the complexities of configuring drivers, database schema, and security settings. 

[Neon](https://neon.tech/?ref=hm) is an open-source, fully managed, serverless Postgres. Neon separates storage and compute, provides real-time scaling, and allows for easy branching of databases. Its scale-to-zero feature ensures efficient resource allocation and offers a generous free tier that makes it cost-effective. Neon also substitutes the PostgreSQL storage layer by redistributing data across a cluster of nodes, which sets it apart from conventional setups. 

This tutorial will guide you through setting up Keycloak with Neon Postgres, including best practices and troubleshooting guidelines for a secure and high-performing application.


## Prerequisites
To properly understand how to set up Keycloak and Postgres, you need to meet the following requirements:

- Installation of the latest version of [Java](https://www.openlogic.com/openjdk-downloads).
- [Docker container](https://www.docker.com/products/docker-desktop/?utm_source=hackmamba&utm_medium=blog&utm_id=HMBcommunity) installed.
- [PostgreSQL](https://www.postgresql.org/download/?utm_source=hackmamba&utm_medium=blog&utm_id=HMBcommunity) installed on the machine. Read this [guide](https://www.guru99.com/download-install-postgresql.html) on how to do so.


## Setting up Keycloak with Postgres using Neon

This section is split into three distinct sections:
- **Setting up Neon**
- **Installing Postgres**
- **Setting up Keycloak with Neon**

### Setting up Neon
**Step 1:** Sign up for Neon:
Visit [Neon](https://console.neon.tech/sign_in) to create an account

![Neon signup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6c0hdldop8wdrahcsgya.png)

**Step 2:** Create a database:
Follow [Neon's instructions](https://neon.tech/docs/get-started-with-neon/signing-up?ref=hm) to create a new PostgreSQL database and select the Postgres version.


![Getting started with neon ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlxde0amqs7rxxe78sh2.png)

**Step 3:** Configure database: 
Note the database URL, username, and password you'll need to connect to your application later.


![Neon dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1qx3v8ylf7kcd8eb598d.png)

**Step 4:** Create tables via SQL queries:
Type in the following command in your Neon SQL editor to create a signup table:

```js
create table signup(
    id serial primary key, 
    fullname text not null,
    username text not null
);
```
Then, add some data to the table:

```js
insert into signup (fullname, username) values
  ('Peace Okafor', 'Ladyprowess'), ('Esther Okafor', 'ada');
```
On the Neon dashboard, go to tables and click the database you just created to see the data.


![Neon table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eaarxe0ve2wjgefd5b86.png)

### Installing Postgres locally

**Step 1:** Install PostgreSQL (if not already installed):
Download it from the [official PostgreSQL website](https://www.postgresql.org/download/?utm_source=hackmamba&utm_medium=blog&utm_id=HMBcommunity). Type `psql --version` in the terminal. This command returns the version of psql if it's installed correctly.


![Install postgresql](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sby3bppcod4wmiro8gj5.png)


**Step 2:** Access Neon Postgres:
Use the provided connection string to connect to your Neon Postgres database. You can do this via a PostgreSQL client or through a terminal:

```js
psql "postgresql://Ladyprowess:*********@ep-green-recipe-74532497.us-east-2.aws.neon.tech/login?sslmode=require"
```

**Note:** In the Windows command prompt, you should use double quotes for the connection string instead of single quotes. 

Nice! You've successfully set up your Neon Postgres database using psql locally.

Here are a few basic commands to get started to verify your connection:


-  `\l` or `\list` to list all database

![List database](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/unqamo7tvqbbn3unda6x.png)

- `SELECT * FROM users;` to select everything from a table named `users`. You can change the table user to the one you set from your SQL Queries.


![Neon postgres table SQL queries](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e37oj99bnrl02ty0twwe.png)

### Setting up Keycloak

**Step 1:** Download Keycloak (if not already installed):
Visit the Keycloak [website](https://www.keycloak.org/downloads.html?utm_source=hackmamba&utm_medium=blog&utm_id=HMBcommunity) and download the latest version of Keycloak. It's usually provided as a .zip or .tar.gz file.



![Download Keycloak](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rtx7qi4us7hzbiw6lb1k.png)


**Step 2:** Extract the files
Extract the downloaded archive to a directory of your choice.

![Extract Keycloak](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdw2o1gjqkr5enfdufjy.png)

## Configuring Keycloak to use Neon Postgres
Step 1: Configure Neon Postgres in Keycloak:
Locate the `keycloak.conf` file in the keycloak folder and edit the following settings with the Neon Postgres connection provided. This is what it’ll look like:

```js
# Basic settings for running in production. Change accordingly before deploying the server.
# Database
# The database vendor.
db=postgres
# The username of the database user.
db-username=Ladyprowess
# The password of the database user.
db-password=*********
# The full database JDBC URL. If not provided, a default URL is set based on the selected database vendor.
db-url=jdbc:postgresql://ep-green-recipe-74532497.us-east-2.aws.neon.tech/login
# Observability
...
```
Note: The replaced URL for your Neon Postgres JDBC should look something like **`_db-url=jdbc:postgresql://ep-green-recipe-74532497.us-east-2.aws.neon.tech/login_`**. However, if SSL is required for your Neon Postgres connection, add it to the JDBC URL: **`_db-url=jdbc:postgresql://ep-green-recipe-74532497.us-east-2.aws.neon.tech/login?sslmode=require_`**.

**Step 2:** Start Keycloak:
Open a terminal or command prompt and navigate to the directory where you extracted Keycloak.

```js
cd <keycloak folder name>
```

Next, open the Docker container and log in the run keycloak with docker using the command prompt:

```js
docker run -p 8080:8080 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin jboss/keycloak
```

**Note:** You can change **admin** to a more secure username and password for production environments. Also, check Docker's availability and status before running the Keycloak container.

![Start Keycloak in Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mvpn078wcvgn5rrzqf1n.png)

This command starts a Keycloak server with the admin username and password set to admin. Access it via http://localhost:8080.

![Run keycloak in browser](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/555b3mh1sr8sd58menvb.png)

Click on the **Administration Console** to log in with your username and password.

![Sign in to Keycloak](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zujflv4l185qee4ckvgp.png)


## Verifying the setup

To test the connection, go to your command prompt and type 

```js
\dt
```
{% embed https://www.loom.com/share/6de3bd359cfa484389813c58fdfb59cb?sid=06783e99-8d0f-4aa4-80f5-b0baf811f27c %}

Viola! You have successfully set up Keycloak and Postgres using Neon!

Additionally, you can verify the connection to the Postgres database through Keycloak.


1. Verify that the changes made in Keycloak (like a new user) are reflected in the database. This confirms that Keycloak is connected and able to modify the database.
2. In the Keycloak Admin Console, try performing an operation requiring database access, such as creating a new user or realm. If you can perform these operations without errors, it implies that Keycloak successfully communicates with the Neon Postgres database.
3. Inspect the server logs for any errors related to database connectivity. If Keycloak starts without any database connection errors, it's a good sign that the connection is successful. Keycloak logs typically show errors if they cannot connect to the database, like connection timeouts, authentication failures, or configuration issues.


## Tips and troubleshooting guide

Here are some tips and troubleshooting ideas that could be useful when setting up Keycloak and Postgres using Neon:

**Keycloak on Neon and Postgres: Tips for installation and use**

1. **Ensure compatibility:** Ensure that all components (Keycloak, PostgreSQL, Neon) are compatible with each other and your system before you start.
2. **Use secure credentials:** Use secure and unique passwords for the database and Keycloak admin accounts in production environments.
3. **Backup regularly:** Make a backup copy of your database and the Keycloak configuration to avoid data loss.
4. **Keep software updated:** Always update Keycloak, Postgres, and other software and ensure you have the latest security patches and features.
5. **Monitor performance:** Keep checking on the performance of both Keycloak and Postgres. Tune configurations for top-notch performance.
6. **Leverage Neon features:** Take advantage of Neon’s cloud-native characteristics, such as automatic scaling and increased security functionalities.
7. **Document your configuration:** Maintain a log of your setup and configuration settings. This is very crucial for troubleshooting and as a reference.

## Troubleshooting common issues

1. **Connection issues:** Ensure the right connection string is specified and your network can connect to the database service.
2. **Authentication problems in Keycloak:** If you experience login problems with Keycloak, check the user credentials and see if the Keycloak server is working correctly.
3. **Database connection errors:** Ensure that the JDBC URL used in the Keycloak configuration points to the Neon Postgres database correctly. Also, ensure that the database is running and is accessible.
4. **Docker container issues:** Ensure that the Docker is installed correctly and running well if the Keycloak Docker container is not working properly. Look for any error messages when starting the container.
5. **SSL configuration errors:** Ensure the SSL mode is set in the JDBC URL and all needed certificates are available if you apply SSL.
6. **Table creation or Data insertion errors:** Verify the SQL syntax and ensure the user has the necessary privileges in the database.
7. **Performance issues:** If there is lag or slow performance, look into the database and server logs for any errors or bottlenecks that may be causing the problem—improved resource utilization through adjustment of configurations. 

Use the command prompt to check logs:

```js
docker logs [keycloak_container_id]
```

## Conclusion

In summary, integrating a PostgreSQL database with Keycloak Neon is a reliable strategy for handling identity and access on the web. In this article, we walked through setting up the components step-by-step. Following this guide, you can use this integration's performance, scalability, and security gains.

The setup we covered ensures a secure and reliable identity management system and lays the foundation for you to scale and optimize your application’s performance. As technology develops, this integration provides a stable basis for adaptation and advancement of functionality to store users’ information securely and effectively.

## References
- [Connect with psql](https://neon.tech/docs/connect/query-with-psql-editor?ref=hm)
- [Login with Keycloak](https://supabase.com/docs/guides/auth/social-login/auth-keycloak?utm_source=hackmamba&utm_medium=blog&utm_id=HMBcommunity)
- [Setup PostgreSQL database for keycloak](https://nagasudhir.blogspot.com/2023/05/setup-postgresql-database-for-keycloak.html)