In This Guide, We will create our first API which is user registration API using rust web powerful framework actix-web, sqlx, and postgresql. Let's start by installing and setting up postgresql.

Let's walk you through the process of installing PostgreSQL on various operating systems, setting up a database, and creating a table for an e-commerce application. Each command is explained to help beginners understand what they are doing.

To install and set up PostgreSQL, follow these steps based on your operating system:

1. Install PostgreSQL

On Ubuntu/Debian:

Update your package list:

Install PostgreSQL:

Start and enable the PostgreSQL service:

On CentOS/RHEL:

Enable the PostgreSQL repository:

Install PostgreSQL:

Initialize the database:

Start and enable the PostgreSQL service:

On macOS (using Homebrew):

Install PostgreSQL:

Start PostgreSQL:

On Windows:

2. Set Up PostgreSQL

Access the PostgreSQL Shell:

Create a New User and Database:

Step 2: Design the ecommerce_users Table

Here’s the schema for the ecommerce_users table with a UUID primary key:

Once The User 'usama' has been assigned roles and permissions to create new tables and data in 'ecomdb'. You can use below command to login to your database

psql postgres://username:password@localhost/db_name

My username is 'usama',

password is 'usama987'

db_name is 'ecomdb'

As above mentioned I am using a unique identifier (UUID) as primary key of my table therefore UUID-ossp extension should be created in our database

Finally I can create my first table ecommerce_users

\dt

Prerequisites

Before starting, ensure you have the following installed:

Step 1: Setting Up the Project

Add dependencies:

Open Cargo.toml and add the following dependencies:

These dependencies include:

Create a .env file:

This File name is .env (It has no extension). It is used to manage environment variables for project.

Create a .env file in the root of your project with the following content:

DATABASE_URL=postgres://username:password@localhost/db_name

Replace username, password, and db_name with your PostgreSQL credentials and database name. In my case it will written as

Step 3: Writing the Code

3.1. Database Connection Pool (src/db.rs)

Create a file src/db.rs to handle the database connection pool:

This function creates a connection pool to the PostgreSQL database using the DATABASE_URL environment variable.

Step-by-Step Explanation:

1. Imports (use statements):

2. Function Definition:

3. Getting the Database URL:

4. Creating the Connection Pool:

5. Returning the Pool:

Key Concepts Explained:

3.2. Models (src/models.rs)

Create a file src/models.rs to define the data structures:

This file defines two structs:

Step-by-Step Explanation:

1. Imports (use statements):

2. Defining the User Struct:

3. Defining the NewUser Struct:

Key Concepts Explained:

3.3. Authentication Handler (src/authentication.rs)

Create a file src/authentication.rs to handle user registration:

This function:

Step-by-Step Explanation:

1. Imports (use statements):

2. Function Definition:

3. Database Query:

4. Handling the Query Result:

Key Concepts Explained:

3.4. Main Application (src/main.rs)

Finally, update src/main.rs to set up the Actix-Web server:

Step-by-Step Explanation:

1. Module Declarations:

2. Imports (use statements):

3. Main Function:

4. Loading Environment Variables:

5. Creating the Database Connection Pool:

6. Setting Up the Actix-Web Server:

If you have successfully started your server, Let's Try to invoke this API by using below command

Key Concepts Explained:

Congratulations! 🎉 You’ve successfully built a user registration API using Actix-Web and PostgreSQL. Let’s recap what we’ve accomplished and the key concepts you’ve learned along the way.

What We Built:

Key Concepts You Learned: