Andy Crouch - Code, Technology & Obfuscation ...

Setting Up PostgreSQL On Arch

Man Looking At Monitor

Photo: Austin Distel - Unsplash

Today I installed PostgreSQL on my local machine to work through some tutorials for a new language. (More on that soon). It wasn’t the most straightforward install and set up ever. Some of the steps appear to be missing or different from the Arch wiki and so I thought I would document it here.

First up install PostgreSQL in pacman with

$ sudo pacman -S postgresql

Next, you need to initialise your database storage cluster on disk. This will be the directory which stores all the data. There is no default location but most people stick with the convention of mapping it to /var/lib/postgres/data. You can initialise this with

$ sudo mkdir /var/lib/postgres/data

You then need to set the owner of that directory to be the PostgreSQL user with

$ sudo chown -c -R postgres:postgres /var/lib/postgres

Next we need to switch to the PostgreSQL user and initialise a database cluster which we do with

$ sudo -i -u postgres
$ initdb -D '/var/lib/postgres/data'

Once completed you can log out and start PostgreSQL with

$ logout && sudo systemctl start postgresql

If you want PostgreSQL to start each time you launch your machine then run

$ sudo systemctl enable postgresql

The final thing to do is to grant your usual user access to save you having to keep switching to the PostgreSQL user to access the PostgreSQL shell which you can do with

$ createuser -s -U postgres --interactive
  Enter name of role to add: YourUsualLoginUserName

At this point, you should have a functioning PostgreSQL install.

I’m now looking for a good GUI for PostgreSQL. Please share your recommendations with me via twitter or email.