Creating a Superuser Role in PostgreSQL

What do you do when you have an error like this: "FATAL: role "root" is not permitted to log in."

Suppose you are getting the error "FATAL: role "root" is not permitted to log in" when you are using PostgreSQL in a docker container (but not necessarily in a container). You must set up a user/role with the necessary permissions, database, and password (to log in outside the container). Let's begin with the PostgreSQL installation from docker. Following the Postgres page in Docker Hub, one of the currently supported versions of PG containers is 13.3-alpine (hint about docker containers: prefer using Alpine Linux images because they are thin, basically, smaller than any other image. It could have only 5MB as the base image). Now, we can create and access a local container from this image by running (considering you have already set up docker on your machine):

docker run --name some-postgres -e POSTGRES_PASSWORD=some-difficult-THING-55922--- -d postgres:13.3-alpine
docker start some-postgres
# -it stands for interactive terminal
docker exec -it some-postgres /bin/bash
psql -U postgres # Sometimes, you need to run `su - postgres` to log in as the Postgres user
CREATE USER root WITH SUPERUSER LOGIN; -- Create a Role with a couple of basic privileges
ALTER USER root PASSWORD 'S0mething...';
\q
createdb -U root root
exit
exit
              

Now, we can connect from the host to the PostgreSQL server in that docker container. If you are using Ubuntu 20.04 and you don't have the Postgres-client, you'll need to set up a PPA-repository for your package manager (considering it is Apt, and following this source):

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" | sudo tee  /etc/apt/sources.list.d/pgdg.list
# We are going to install the client only. It's okay to have the server. On Mac OS X, you must install both and turn off the Postgresql service with Homebrew.
sudo apt update
sudo apt install postgresql-client-13
              

Consequently, we can get the hostname from the PostgreSQL container like this:

HOSTNAME=$(docker exec -it some-postgres hostname -i)
              

Finally, you can connect and access the controller (it will ask you for the password-defined above).

psql -U root -h "${HOSTNAME::-1}" # There is a way to set up the container to be connected with the localhost through a specific port, but let it be for another article.
# The::-1 is necessary because the output from docker adds a new line symbol at the end of HOSTNAME.
              

Done! These steps are enough to connect and run the console in the container from your local machine (the host). Tell us about your journey in the comments!

Portfolio

I selected a few projects I've worked on to show you. Some projects have contract constraints so that I won't be showing them. I played the role of Frontend and Backend developer in all of them, sometimes in time, sometimes on my own.

Lillio's Observations Project: 2024

Lillo's Observations

At Lillio, I served as both the tech lead and full-stack engineer for the Observations project, developing a comprehensive feature using Ruby on Rails, ReactJS, React Native, PostgreSQL, and AWS. I managed the integration of a robust backend with a dynamic frontend to enhance functionality and mobile integration. More info here.

Lune: 2023

Conserve Piso

Payment management system for Cappta getway. This project was done in dotnet core, MSSQL SERVER and VueJS.

Ideallang English: 2022

Ideallang

English Course website made in WordPress and using a MySQL database (in partnership with another engineer).

Posto7: 2021

Ideallang

Nutrition application made with ReactJS, Firestore DB, and Firebase. My main attribution was to build an admin portal for the back office team

PSMI: 2020

PSMI

(Work in maintenance) Education platform containing a bunch of curses in Brazil, written in Ruby on Rails, PostgreSQL, Express JS, Google Cloud Functions, and AWS. I was responsible for upgrading Ruby and Rails versions, as well as transitioning the cloud infrastructure from GCP to AWS.

Mangue Tecnologia: 2020

Mangue Tecnologia

(Work in maintenance) An educational platform offering a variety of courses in Brazil, developed using Ruby on Rails, PHP, JavaScript, and MySQL. I built several eCommerce websites, enhancing user experience and functionality.

Chefstrela': 2019

Ideallang

Gastronomy E-commerce made in WordPress and using a MySQL database (in a partnership with another engineer and a designer)

Casa Roberto Marinho: 2018

ICRM

Content management system with Ruby on Rails, MySQL, and JQuery. Cultural and educational business.

Kariri Sapuyá: 2018

Kariri Sapuyá

Digital archive combined with a content management system for storing and : was delivering historical documents about South American Native peoples in Northeast Brazil. It is made with Ruby, Ruby on Rails, ElasticSearch, JQuery, MySQL, and MongoDB.Link to the web app: Kariri Sapuyá.

Perceive: 2017

Perceive

Automated tool for extracting useful information about vulnerabilities from mailing lists. It includes a series of solutions, including Python Jupyter Notebooks, a Flask server, and a MySQL database. More info here.

Conserve Piso: 2017

Conserve Piso

A web system developed using PHP, MySQL, and jQuery, designed to streamline the management of flooring projects. This system replaces traditional methods with a digital platform that enhances efficiency and accuracy.

DPMS (Digital Management System): 2015-2017

DPMS

System constructed with Ruby on Rails, MySQL, JQuery, and Telerik Kendo. It aims to manage all steps of an industrial project, replacing common spreadsheets with sophisticated, customizable, and reliable resources. See more info: See more info: Oxeanbits.

Portuguese Atlantic (Portuguese Name: Atlântico Português): 2014

Portuguese Atlantic

Content management system made with Ruby on Rails, PostgreSQL, JQuery, and ElasticSearch. It is an application to manage and search content in a digital historical archive, which is also uploaded within the CMS. Link to the web app: Atlântico Português.

About me

Hey! I'm a senior full-stack software engineer, Vinicius L. Gesteira (aka Vini). Since 2014, I've worked with web development (mainly Ruby, Ruby on Rails, Rust, Solidity, Node JS, Docker, AWS, React, and Svelte), machine learning, web scraping, Python scripts and other related fields. If you are interested in more details, please check out my LinkedIn and GitHub.