PostgreSQL: Creating a user and database in PostgreSQL

This documentation is part of the Getting started guide. View the full guide here: How to get started with PostgreSQL.

👋 Welcome to the Stackhero documentation!

Stackhero provides a fully managed PostgreSQL cloud service, designed for reliability and speed:

  • Unlimited connections and data transfers for seamless scaling.
  • PgAdmin web UI included for intuitive database management.
  • Popular extensions included, such as PostGIS, TimescaleDB, and PgVector.
  • Simple, one-click updates keep your database secure and current.
  • High performance and strong security on private, dedicated infrastructure.

You can get started with Stackhero's PostgreSQL cloud hosting in just a few minutes. Enjoy a reliable database experience and save valuable time managing your data.

By default, an admin user is created with administrative rights. It is a good practice to create a dedicated user and database for each project you plan to host.

To use the PgAdmin web UI, open your PostgreSQL domain with HTTPS (for example, https://<XXXXXX>.stackhero-network.com). Log in with admin as the username and the password that you set in your service configuration (visible in your Stackhero dashboard).

  1. Go to Servers / PostgreSQL, right-click on Login/Group Roles, and select Create / Login/Group Role:

    Create a user in PostgreSQL using PgAdminCreate a user in PostgreSQL using PgAdmin

  2. Set the login name:

    Define user loginDefine user login

  3. Set a secure password to avoid brute force attacks:

    Define user passwordDefine user password

  4. Finally, ensure that only the "Can login" privilege is selected:

    Define user rightsDefine user rights

Click the "Save" button to create your user.

  1. Go to Servers / postgresql, right-click on Databases, and select Create / Database...:

    Create a database using PgAdminCreate a database using PgAdmin

A good practice is to use the same name for both the database and the user. For example, if your project is named "superWebsite," consider creating a user named "superWebsite" and a database named "superWebsite".

  1. Set the database name and choose the owner (the user you just created):

    Defined database name and ownerDefined database name and owner

Your database is now created.

To create a user on PostgreSQL using the psql CLI, run the following SQL query:

CREATE ROLE "myProject" WITH
  LOGIN
  NOSUPERUSER
  NOCREATEDB
  NOCREATEROLE
  NOINHERIT
  NOREPLICATION
  CONNECTION LIMIT -1
  PASSWORD 'secretPassword';

Don't forget to replace myProject with your project name and secretPassword with a secure password. Also, it is a good practice to use your project name as both the login and database name. If your project name is "superWebsite," consider creating a user named "superWebsite" and a database named "superWebsite".

You can generate a secure password with this command line: openssl rand -base64 24 | tr -d '\n' | cut -c1-32

To create a database on PostgreSQL using the psql CLI, run the following SQL query:

CREATE DATABASE "myProject"
  WITH
  OWNER = "myProject"
  ENCODING = 'UTF8'
  CONNECTION LIMIT = -1
  IS_TEMPLATE = false;

A good practice is to use the same name for both the database and the user. For example, if your project name is "superWebsite," consider creating a user named "superWebsite" and a database named "superWebsite".