Unable to connect to postgres

Hi, I’m building the appointment scheduler and for a few days now I’ve been getting this message when I try to connect to psql:
codeally@c57ab2b73578:~/project$ psql --username=freecodecamp dbname=postgres
psql: error: connection to server on socket “/var/run/postgresql/.s.PGSQL.5432” failed: No such file or directory
Is the server running locally and accepting connections on that socket?
In the other terminal I get some equivalent message when trying to run my bash script.
Is this a problem with codeally or is it something I can fix on my machine?
Thanks a lot!
Fco

1 Like

Oh, is this what the learn outage announcement is all about? In that case, I’m sorry for asking…

It might be related to one of the outages, freeCodeCamp and CodeAlly both had some issues recently - as well as updates.

Enter these commands in the terminal:

  1. sudo cp ./.freeCodeCamp/pg_hba.conf /etc/postgresql/12/main/pg_hba.conf
  2. sudo chown -R postgres:postgres /var/lib/postgresql/12/main
  3. sudo service postgresql restart
  4. echo "SELECT 'CREATE USER freecodecamp WITH CREATEDB' WHERE NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname='freecodecamp')\gexec" | psql -U postgres -X

They’re the commands that run when you start that project to set things up. Try to login again after that. Let me know if it works.

4 Likes

Yes, worked.
Thanks a lot!

1 Like

I am getting this error in the third command, help please!

Error: /usr/lib/postgresql/12/bin/pg_ctl /usr/lib/postgresql/12/bin/pg_ctl start -D /var/lib/postgresql/12/main -l /var/log/postgresql/postgresql-12-main.log -s -o  -c config_file="/etc/postgresql/12/main/postgresql.conf"  exited with status 1: 
pg_ctl: another server might be running; trying to start server anyway
 FATAL:  could not remove old lock file "postmaster.pid": Read-only file system
 HINT:  The file seems accidentally left over, but it could not be removed. Please remove the file by hand and try again.
pg_ctl: could not start server
Examine the log output.

In my case sudo service postgresql restart did not work. I got pg_ctl: could not start server

codeally@c04db3918a02:~/project$ sudo service postgresql restart
 * Restarting PostgreSQL 12 database server                                                                                                           * Error: /usr/lib/postgresql/12/bin/pg_ctl /usr/lib/postgresql/12/bin/pg_ctl start -D /var/lib/postgresql/12/main -l /var/log/postgresql/postgresql-12-main.log -s -o  -c config_file="/etc/postgresql/12/main/postgresql.conf"  exited with status 1: 
2022-09-14 14:35:39.122 UTC [3495] LOG:  starting PostgreSQL 12.9 (Ubuntu 12.9-2.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, 64-bit
2022-09-14 14:35:39.122 UTC [3495] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2022-09-14 14:35:39.122 UTC [3495] LOG:  could not bind IPv6 address "::1": Cannot assign requested address
2022-09-14 14:35:39.122 UTC [3495] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2022-09-14 14:35:39.130 UTC [3495] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-09-14 14:35:39.155 UTC [3496] LOG:  database system shutdown was interrupted; last known up at 2022-09-14 14:32:27 UTC
2022-09-14 14:35:39.199 UTC [3496] LOG:  database system was not properly shut down; automatic recovery in progress
2022-09-14 14:35:39.204 UTC [3496] LOG:  redo starts at 0/1829EB8
2022-09-14 14:35:39.204 UTC [3496] LOG:  invalid record length at 0/182A288: wanted 24, got 0
2022-09-14 14:35:39.204 UTC [3496] LOG:  redo done at 0/182A210
2022-09-14 14:35:39.231 UTC [3496] PANIC:  could not fdatasync file "000000010000000000000001": Input/output error
2022-09-14 14:35:39.337 UTC [3495] LOG:  startup process (PID 3496) was terminated by signal 6: Aborted
2022-09-14 14:35:39.337 UTC [3495] LOG:  aborting startup due to startup process failure
2022-09-14 14:35:39.338 UTC [3495] LOG:  database system is shut down
pg_ctl: could not start server
Examine the log output.

Then I had also to run sudo chmod 644 /etc/postgresql/12/main/pg_hba.conf

Source error:

psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
        Is the server running locally and accepting connections on that socket?

I’m having the same issue as listed above and on another topics.
I’ve tried the solutions included in this current topic and the 2 others below, but neither of them have worked.

It started all of a sudden yesterday, when I was working on Build a Mario Database lesson. I’ve waited until today to see if it wasn’t a bug that would be patched, but until now I keep getting the same error.

When following the step 2 on the comment below, I get a Bad command error.

1 Like

Hi there!
I have same problem too and I tried all of these solutions listed above. I’m stuck in Periodic Table Database challenge. Please help, it seems like postgresql doesn’t work.

1 Like

I’m getting the same error, I’m working on build worldcup database. Need solutions on this apart from the above stated.

1 Like

Hello,

I have same problem in Periodic Table Database:

psql: error: connection to server on socket “/var/run/postgresql/.s.PGSQL.5432” failed: No such file or directory
Is the server running locally and accepting connections on that socket?

Please help

Hello, thank for the effort!

I try but non work for this similar issue:

Thank you!

The solution was found here under “Last resort:…”

I have the same problem and the solution you give here does not work, it gives an error in the first line

I’m having similar issues and there seems to be no fix.

1 Like

I recently am experiencing the same problem while completing "Learn SQL by Building a Student Database: Part 2 ":
psql: error: connection to server on socket “/var/run/postgresql/.s.PGSQL.5432” failed: No such file or directory
Is the server running locally and accepting connections on that socket?
There’s no apparent fix and it won’t connect to the POSTGRESQL server.
Please anyone with knowledge on how to fix this?

Hello I ran into the same problem and after digging through I found a solution which I think will help;

1.first I went through all ouput logs
cd /home/codeally
ls
less pg.log

from the logs I found that there was an error at line 94

which the issue was “LOG: could not bind IPv6 address “::1”: Cannot assign requested address”
so I changed the code at line 94 to “# IPv6 local connections:
host all ::1/128 trust”

then restarted postgres with “sudo service postgresql restart”
finally connected the psql with " psql --username=freecodecamp --dbname=postgres"
and worked

I hope it will be helpful :smile:

Hi, I’m also having this issue:

It started when I reached around 75% in the Mario database tutorial.

I tried the above solution, but there isn’t a file called pg.log in that location for me.

If it matters, I’m running these tutorials in VS Code since the browser version is not working for me.

Hello ,can you try rebuilding the container and starting the tutorial again.I think your progress isn’t saved when running locally.I had to start over again my Mario database project :frowning_face:

OMG, the frustration has ended. Resetting the tutorial and rebuilding the container was the first thing I did last week (when I first encountered this error). Since then, I’ve been frantically googling and trying multitudes of different solutions with no success.

Your message prompted me to completely restart the tutorial and rebuild the container yet again, and for some reason, it worked this time. So, THANK YOU, william!!