PostgreSQL

Jul 28, 2024

I suggest PostgreSQL when someone asks for the recommendation on what database one should use. I use PostgreSQL when I am learning or building something in a new web framework or library. I use PostgreSQL when I want to experiment with databases. PostgreSQL is the default choice for me in most of the cases.

Every few months, I change the operating system from one GNU/Linux distro to another mostly staying with the land of Debian (e.g. apt). Most of the time, I just need to run the following command to install PostgreSQL.

sudo apt install postgresql

This command install and activates the PostgreSQL server locally on my computer. I use the psql command tool to work with this database server.

Following are the commands I use to work with PostgreSQL.

sudo -i -u postgres
psql

Although technically it is not correct, I read the first command as sudo imagine user as postgres to provide access. After entering the password, the user changes from the current logged-in user to postgres and then I can directly work with PostgreSQL using the second psql command.

We can combine the above two commands into a single one as follows.

sudo -iu postgres psql

Again, technically it is not correct, but I read it as sudo imagine users as postgres and then launch psql directly.

The first thing I normally do after connecting with PostgreSQL is to change the password. This is trivial as running the following command.

password \postgres

We are changing the password for the postgres user. After running this command, PostgreSQL asks you to enter and re-enter a new password for the postgres user. And then you are ready to go!

-–

If somehow, you are facing difficulties installing PostgreSQL on your computer, installing via Docker is yet another option. At some convenient place, save the docker-compose.yml file with following content.

version: '3'

services:
  db:
    image: postgres
    restart: always
    ports:
      - '5432:5432'
    environment:
      POSTGRES_PASSWORD: pq2q4a

You can choose any other password you like instead of pq2q4a!

Once done, run the following command in the terminal where you have this docker-compose.yml file.

docker-compose up -d

This will run the docker-compose.yml file and up the PostgreSQL service in detached mode. Meaning that it’ll run the PostgreSQL service in the background once the docker is running. When you start the computer, if Docker is not running, you just need to run the docker and it’ll auto run the PostgreSQL service for you.

Tags: psql