DEV Community

Cover image for How to set up Keycloak and Postgres using Neon
Okafor Peace Ngozi for Hackmamba

Posted on

How to set up Keycloak and Postgres using Neon

Keycloak 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 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 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:

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 to create an account

Neon signup

Step 2: Create a database:
Follow Neon's instructions to create a new PostgreSQL database and select the Postgres version.

Getting started with neon

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

Neon dashboard

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



create table signup(
    id serial primary key, 
    fullname text not null,
    username text not null
);


Enter fullscreen mode Exit fullscreen mode

Then, add some data to the table:



insert into signup (fullname, username) values
  ('Peace Okafor', 'Ladyprowess'), ('Esther Okafor', 'ada');


Enter fullscreen mode Exit fullscreen mode

On the Neon dashboard, go to tables and click the database you just created to see the data.

Neon table

Installing Postgres locally

Step 1: Install PostgreSQL (if not already installed):
Download it from the official PostgreSQL website. Type psql --version in the terminal. This command returns the version of psql if it's installed correctly.

Install postgresql

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:



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


Enter fullscreen mode Exit fullscreen mode

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

  • 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

Setting up Keycloak

Step 1: Download Keycloak (if not already installed):
Visit the Keycloak website and download the latest version of Keycloak. It's usually provided as a .zip or .tar.gz file.

Download Keycloak

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

Extract Keycloak

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:



# 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
...


Enter fullscreen mode Exit fullscreen mode

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.



cd <keycloak folder name>


Enter fullscreen mode Exit fullscreen mode

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



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


Enter fullscreen mode Exit fullscreen mode

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

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

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

Sign in to Keycloak

Verifying the setup

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



\dt


Enter fullscreen mode Exit fullscreen mode

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:



docker logs [keycloak_container_id]

Enter fullscreen mode Exit fullscreen mode




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

Top comments (1)

Collapse
 
krlz profile image
krlz

great, I even didn't know about Neon I will check it out