Creating a PostgreSQL database on a server terminal

Access the PostgreSQL Terminal

Log in to the PostgreSQL command-line interface using the following command:

sudo -u postgres psql

This assumes you’re logged in as a user with sudo privileges and postgres is the PostgreSQL administrative user.

Create a Database

In the psql terminal, run:

CREATE DATABASE database_name;

Verify the Database Creation

You can list all the databases to confirm:

l

Optional: Assign a User to the Database

If you want a specific user to own the database, you can specify it while creating the database:

CREATE DATABASE database_name OWNER username;

Or change ownership of an existing database:

ALTER DATABASE database_name OWNER TO username;

Exit the PostgreSQL Terminal

Type q to exit.

Connect to the Database

You can connect to the newly created database directly:

psql -d database_name

Leave a comment

Your email address will not be published. Required fields are marked *