How to install Postgres database in Ubuntu?
To install PostgreSQL on Ubuntu, you can follow these steps:
-
Open a terminal window on your Ubuntu machine.
-
Update the package list and upgrade existing packages by running the following commands:
sudo apt update
sudo apt upgrade
- Install PostgreSQL by running the following command:
sudo apt install postgresql postgresql-contrib
- 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
- 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
- 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:
- 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.
- 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.
- 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.
- Exit the PostgreSQL command prompt by running:
\q
- Now you can setup your ORM database url environment like this.
DATABASE_URL="postgresql://username:password@localhost:5432/mydatabase"