Understanding Postgres on Linux

Installing Postgres

Installing PostgreSQL is pretty straight-forward on Linux, just follow a tutorial like this one:

Where I confused the first time around was why certain things worked the way they did so this post is to help address some of those issues.

The Postgres User

When you first install Postgres on Linux it creates a new Linux user called Postgres with superuser privileges. Why does this matter?

  • When you run “psql” command to run the Postgres shell, it defaults to opening the shell under a username that matches the current Linux users and opens a database by the same name. Unless you installed Linux and your first user was called “postgres” then this command is going to give you an error.

To fix this issue we need to switch over to the postgres user.

sudo su postgres

Creating a New User and Database

Once you are working in the terminal under the postgres user do the following.

  1. enter the postgres shell psql
  2. create a new superuser

CREATE USER <YourLinuxUsername> WITH SUPERUSER PASSWORD “somepassword”;

  1. create a new database

CREATE DATABASE <YourLinuxUsername>;

  1. quit postgres

Port 5432

The default port for postgres is 5432 in the same way the default port for mongo is 27017.

Other Notes

  • Postgres drivers in many languages are compiled from source by the package manager, if installing the database drivers in that language fail, you may need to download Xcode on mac or install python3-dev libpq-dev on Linux.
  • On PHP you need to find your PHP installs php.ini and uncomment the pg drivers
  • Your local postgres connection URL will look like: postgres://USERNAME:PASSWORD@localhost:5432/databasename (unlike Mongo, the database does need to be created before you can connect to it)




Alex Merced is a Full Stack Developer, learn more about his work at AlexMercedCoder.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

CS373 Spring 2022: Matthew Kozlowski: Final Entry

Going Cloud Native with AWS Elastic Container Service

Flutter: Google Translate part 2

Our Kubernetes journey at TheFork

Microsoft OneNote vs Evernote

All About Bloom Filter

Podcasts that I listened to this Month — Article (2)

Keeping Up with the Fans: Scaling for Big Events at Whatnot, with Elixir and Phoenix

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alex Merced Coder

Alex Merced Coder

Alex Merced is a Full Stack Developer, learn more about his work at AlexMercedCoder.com

More from Medium

Build Your Own Docker Registry UI With Pythons Flask

Deploy Containerized Flask app to Heroku

Docker for beginners

Forking and Pushing Changes to a Remote Repository using Git and GitHub