How to install Postgres database in Ubuntu?

To install PostgreSQL on Ubuntu, you can follow these steps:

  1. Open a terminal window on your Ubuntu machine.

  2. Update the package list and upgrade existing packages by running the following commands:

sudo apt update
sudo apt upgrade
  1. Install PostgreSQL by running the following command:
sudo apt install postgresql postgresql-contrib
  1. After installation, PostgreSQL should start automatically. You can check its status by running:
systemctl status postgresql

If it's not running, you can start it by running:

sudo systemctl start postgresql
  1. By default, PostgreSQL creates a user called "postgres" with administrative privileges. To set a password for this user, run the following command:
sudo passwd postgres
  1. To connect to the PostgreSQL server, use the following command:
sudo -u postgres psql

This will open the PostgreSQL command prompt, where you can execute SQL queries.

Create user and password for database

To create a user and password for a PostgreSQL database, you can use the following steps:

  1. Connect to the PostgreSQL server as a user with administrative privileges. You can use the following command:
sudo -u postgres psql

This will open the PostgreSQL command prompt.

  1. Create a new user by running the following command:
CREATE USER username WITH PASSWORD 'password';

Replace "username" with the name of the user you want to create, and "password" with a strong password of your choice. Make sure to enclose the password in single quotes.

  1. Grant permissions to the new user. For example, if you want to give the user full access to a database called "mydatabase", you can use the following command:
GRANT ALL PRIVILEGES ON DATABASE mydatabase TO username;

Replace "mydatabase" with the name of your database, and "username" with the name of the user you just created.

  1. Exit the PostgreSQL command prompt by running:
\q
  1. Now you can setup your ORM database url environment like this.
DATABASE_URL="postgresql://username:password@localhost:5432/mydatabase"