DEV Community

Mikhail Grigoriev
Mikhail Grigoriev

Posted on

Resolving PostgreSQL issues when switching OS's

When switching from using Ubuntu on my Virtual Machine to using it as my primary operating system, I ran into a fun error and thought I might as well document my process in case anyone had a similar problem.

I got my system up and running, and cloned a Rails app I was working on previously. After running bundle install the local server seemed to boot up, but openiing the browser I saw this fun error:

PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

After a couple of hours of scouring the internet to get a better understanding of what was happening, I realized that PGSQL.5432 was referring to a port number - and decided to check if my app's current TCP port on the app's config/database.yml file matched with what postgres was actually running.

So: Navigate to the file, and in the terminal type in pg_lsclusters. They were missmatching! So I corrected the mistake in the database.yml
file, and was greeted with a new error:

We could not find your database: facebook_clone_development. Which can be found in the database configuration file located at config/database.yml.
To resolve this issue:
Did you create the database for this app, or delete it? You may need to create your database.
Has the database name changed? Check your database.yml config has the correct database name.
To create your database, run:
bin/rails db:create

Running the database creation, I was now told that ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: permission denied to create database

Progress! This gave me an idea based off of the information I read thus far about PostgreSQL trying to wrap my head around the problem: A user, with the proper
privileges, and a password, are normally needed to create a database.

So away we go, starting with sudo -u postgres createuser mgrigo. This uses postgres to create the user you wish to use to create the database. Next, let's give ourselves the ability to create databases ALTER USER mgrigo CREATEDB;. Now we should be able to create the database!

Hope back into your terminal, and run rails db:create, which should create the database. Now all you need to do is migrate your database, and you should be all set.

Starting out I didn't fully understand the complexity of PostgreSQL, but this experience definitely humbled me and gave me a newfound appreciation for it.

Top comments (0)