AskHandle

AskHandle Blog

How To Create a PostgreSQL Database and Manage Users

September 6, 2025Annie Hayes3 min read

How To Create a PostgreSQL Database and Manage Users

Creating a PostgreSQL database and managing users is straightforward. This guide will help you create a database named webdata and manage user permissions effectively.

Getting Started with PostgreSQL

Ensure that PostgreSQL is installed and running on your system. You can find installation instructions on the official PostgreSQL website.

Creating Your First Database

Step 1: Open Terminal

Access your terminal application. This can be found in the Utilities folder on macOS and Linux or in Command Prompt/PowerShell on Windows.

Step 2: Log in to PostgreSQL

Log in to the PostgreSQL environment as a user with database creation privileges, typically the 'postgres' superuser:

bash
1psql -U postgres

Step 3: Create the Database

Create a new database named 'webdata':

sql
1CREATE DATABASE webdata;

Step 4: Exit PostgreSQL Prompt

Type \q and press Enter to exit.

Alternatively, you can use the createdb command:

bash
1createdb -U postgres webdata

Creating a New User

After creating your database, you may want to create a new user (or "role" in PostgreSQL).

Step 1: Log in to PostgreSQL

Open your terminal and log in again if you've exited:

bash
1psql -U postgres

Step 2: Create a New User

Create a new user with a password:

sql
1CREATE USER databaseuser WITH PASSWORD 'mypassword';

Replace 'databaseuser' and 'mypassword' with your desired username and password.

Granting Permissions to the User

Now that you have created a new user, you need to grant them the appropriate permissions for the 'webdata' database.

Grant Database Access

Grant the user access to the 'webdata' database:

sql
1GRANT ALL PRIVILEGES ON DATABASE webdata TO databaseuser;

This command grants the user all privileges on the 'webdata' database. Adjust the privileges based on your security requirements.

Grant Table Permissions

Once tables are created in your database, you may want to grant specific permissions on these tables:

sql
1GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO databaseuser;

This command allows the user to select, insert, update, and delete data in all tables within the 'public' schema of the 'webdata' database.

Creating a database and managing users in PostgreSQL is a valuable skill. Following this guide, you have learned how to create a database, create a new user, and grant necessary permissions. Always consider security best practices and back up your data regularly.