Linux
Step 1: Installation
$ sudo apt update
$ sudo apt install postgresql postgresql-contrib
Step 2: Test the installation
sudo -u posgtres psql
You should see:
psotgres=#
Type \q
to quit
Step 3: Create postgres user:
$ sudo -u postgres createuser --interactive
You should see prompts as shown below:
Enter name of role to add: test
Shall the new role be a superuser? (y/n) y
Step 4: Create database
$ sudo -u postgres createdb test
Now, we need to create the same Linux user as the postgres user for authentication to succeed
$ sudo adduser test
now you can log in to your postgres database
$ sudo -u test psql
Step 5: Assign password to the postgres user
$ sudo -u postgres psql
postgres=# ALTER USER test with PASSWORD 'your-new-password';
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
Step 1: Create postgres user:
$ sudo -u postgres createuser --interactive
You should see prompts as shown below:
Enter name of role to add: test
Shall the new role be a superuser? (y/n) y
Step 2: Create database
$ sudo -u postgres createdb test
$ sudo -u test psql
Step 5: Assign password to the postgres user
$ sudo -u postgres psql
postgres=# ALTER USER test with PASSWORD 'your-new-password';
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';
Then, your connection string would be postgres://postgres:your-new-password@localhost:5432/test
Thanks for reading!
Top comments (0)