Install Postgres on Mac/Linux

nedsoft

Chinedu Orie

Posted on June 13, 2020

Install Postgres on Mac/Linux

Linux

Step 1: Installation

$ sudo apt update
$ sudo apt install postgresql postgresql-contrib
Enter fullscreen mode Exit fullscreen mode

Step 2: Test the installation

sudo -u posgtres psql
Enter fullscreen mode Exit fullscreen mode

You should see:
psotgres=#

Type \q to quit

Step 3: Create postgres user:

$ sudo -u postgres createuser --interactive
Enter fullscreen mode Exit fullscreen mode

You should see prompts as shown below:

Enter name of role to add: test
Shall the new role be a superuser? (y/n) y
Enter fullscreen mode Exit fullscreen mode

Step 4: Create database

$ sudo -u postgres createdb test

Enter fullscreen mode Exit fullscreen mode

Now, we need to create the same Linux user as the postgres user for authentication to succeed

$ sudo adduser test

Enter fullscreen mode Exit fullscreen mode

now you can log in to your postgres database

$ sudo -u test psql
Enter fullscreen mode Exit fullscreen mode

Step 5: Assign password to the postgres user

$ sudo -u postgres psql

postgres=# ALTER USER test with PASSWORD 'your-new-password';

Enter fullscreen mode Exit fullscreen mode

Note: test is the name of your postgres user. If you want to connect to the postgres database programmatically you must create a password following step 5.

The connection string for the above database will be postgres://test:your-new-password@localhost:5432/test

Mac

For Mac, what would change is the method of installing postgres

If you already have Homebrew installed, you can install postgress with the command below, if you don't have Homebrew installed click here to install it.

$ brew update
$ brew install postgresql
Enter fullscreen mode Exit fullscreen mode

Step 1: Create postgres user:

$ sudo -u postgres createuser --interactive
Enter fullscreen mode Exit fullscreen mode

You should see prompts as shown below:

Enter name of role to add: test
Shall the new role be a superuser? (y/n) y
Enter fullscreen mode Exit fullscreen mode

Step 2: Create database

$ sudo -u postgres createdb test

Enter fullscreen mode Exit fullscreen mode
$ sudo -u test psql
Enter fullscreen mode Exit fullscreen mode

Step 5: Assign password to the postgres user

$ sudo -u postgres psql

postgres=# ALTER USER test with PASSWORD 'your-new-password';

Enter fullscreen mode Exit fullscreen mode

Note: Creating a new postgres user is optional, you can make do with the default postgres user if you are want.

If you want to use the default posgtres user, all you need to do after installing postgres is:

$ sudo -u postgres createdb test // or  $ createdb test

$ sudo -u postgres psql

postgres=# ALTER USER postgres with PASSWORD 'your-new-password';

Enter fullscreen mode Exit fullscreen mode

Then, your connection string would be postgres://postgres:your-new-password@localhost:5432/test

Thanks for reading!

💖 💪 🙅 🚩
nedsoft
Chinedu Orie

Posted on June 13, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related