CodeAlly not loading/PSQL failures

The error says “connection to server on socket “/var/run/postgresql/s.PSQL.5432” failed: FATAL: codeally does not exist”

There’s no where in the script that I suggest connecting to this server or socket or reference ‘codeally’

#!/bin/bash
# Number Guessing Game

PSQL="psql -X --username=freecodecamp --dbname=number_guessing_game --tuples-only -c" 

# functions
# main guess
MAIN_GUESS() {

read GUESS

if [[ $GUESS =~ ^[0-9]*$ ]]
then

echo "Main guess: $GUESS"
GAME_LOOP $GUESS $SECRET_NUMBER $NUMBER_OF_TRIES $PSQL

else
echo "That is not an integer, guess again:"
MAIN_GUESS
fi
}

# loop function
GAME_LOOP() {
  GUESS=$1
  SECRET_NUMBER=$2
  NUMBER_OF_TRIES=$3
  PSQL=$4

  ((NUMBER_OF_TRIES++))
  echo -e "\nLoop guess: $GUESS\nLoop answer: $SECRET_NUMBER\nLoop tries: $NUMBER_OF_TRIES"
  
  # correct pass
  if [[ $GUESS == $SECRET_NUMBER ]]
  then

  GAMES_PLAYED=$($PSQL "SELECT game_id FROM guessing_game WHERE username = $USERNAME ORDER BY game_id DESC LIMIT 1")
  BEST_GAME=$($PSQL "SELECT score FROM guessing_game WHERE username = '$USERNAME' ORDER BY score DESC LIMIT 1")
  ADD_NEW_PLAYER_RESULT=$($PSQL "INSERT INTO guessing_game(username, score) VALUES('$USERNAME', $NUMBER_OF_TRIES)")
  echo "You guessed it in $NUMBER_OF_TRIES tries. The secret number was $SECRET_NUMBER. Nice job!"
  
  else
  if [[ $GUESS > $SECRET_NUMBER ]]
  then
  echo "It's lower than that, guess again:"
  MAIN_GUESS $GUESS $SECRET_NUMBER $NUMBER_OF_TRIES

  else
  if [[ $GUESS < $SECRET_NUMBER ]]
  then
  echo "It's higher than that, guess again:"
  MAIN_GUESS $GUESS $SECRET_NUMBER $NUMBER_OF_TRIES
  fi
  fi
  fi
}

# start of game test-mode

NUMBER_OF_TRIES=0
echo "test: Number of tries: $NUMBER_OF_TRIES"
SECRET_NUMBER=$(( RANDOM % 1000 +1))
echo "test: Answer is $SECRET_NUMBER"

echo -e "\n~~~~~ Number Guessing Game ~~~~~\n"

# get username
echo "Enter your username:"
read USERNAME

USERNAME_RESULT=$($PSQL "SELECT username FROM guessing_game WHERE username = '$USERNAME'")

# if no username in database
if [[ -z $USERNAME_RESULT ]]
then

# welcome message
echo "Welcome, $USERNAME! It looks like this is your first time here."

else
echo "Welcome back, $USERNAME!"
fi

echo "Guess the secret number between 1 and 1000:"

MAIN_GUESS

Also, there are 6 other forum topics referencing the same error that were posted since August. Unfortunately, I can’t find CodeAlly support…

I’ve deleted the container one more time, just for piece of mind. Still having the issues so I dug into the code further. I’ve made some progress in understanding this issue…

If I remove, or comment, these lines the errors don’t show up

#GAMES_PLAYED=$($PSQL "SELECT game_id FROM guessing_game WHERE username = $USERNAME ORDER BY game_id DESC LIMIT 1")
#BEST_GAME=$($PSQL "SELECT score FROM guessing_game WHERE username = '$USERNAME' ORDER BY score DESC LIMIT 1")
#ADD_NEW_PLAYER_RESULT=$($PSQL "INSERT INTO guessing_game(username, score) VALUES('$USERNAME', $NUMBER_OF_TRIES)")

However, I’ve used similar lines of code in the other projects from this course. I’m once again lost on how to fix this.

I am glad you did that because that is what I would have done also (it is a technique I use when things don’t make any sense to try to block lines out till I have a section of code to focus on)

Question for you, why is the first select using a

While the second uses a

(One without quotes and one with quotes?)

They all should be wrapped in single quotes.

But I’m not getting that syntax mistake as an error. Solely because I haven’t used the $GAMES_PLAYED variable in the script as yet.

my understanding is that because you are calling psql that it is attempting the select (regardless of whether you use the result in the script or not).
What changes if you fix that line?

Also you can try taking the select statements out and running them one by one manually with the current variables all replaced with their values at that moment.
(so instead of trying to run the select, you can echo the select statements out then try to run them)

I made these changes to the code

if [[ $GUESS == $SECRET_NUMBER ]]
  then

  echo -e "Games played: $($PSQL "SELECT game_id FROM guessing_game WHERE username = 'Test2' ORDER BY game_id DESC LIMIT 1")"
  echo -e "Best game: $($PSQL "SELECT score FROM guessing_game WHERE username = 'Test2' ORDER BY score DESC LIMIT 1")"
  ADD_NEW_PLAYER_RESULT=$($PSQL "INSERT INTO guessing_game(username, score) VALUES('Test2', $NUMBER_OF_TRIES)")
  echo "You guessed it in $NUMBER_OF_TRIES tries. The secret number was $SECRET_NUMBER. Nice job!"
  
  else

The results are similar:

Here is one of the queries ran in the database, it works:

if you comment these two out now the error goes away?
(so we know that the insert is good?)

could the quotes be throwing things out? can stuff wrapped in double quotes be nested like this?

try to remove the outer quotes, echo doesn’t need quotes

what if you changed the echo -e lines to simply print out a very simple select
echo -e "$($PSQL "select * from guessing_game limit 2")"

please try also

echo -e $($PSQL "select * from guessing_game limit 2")

No quotes, same result:

Tried it, no change:

can you provide your database dump and your script?

can you take out the where clause in those echo lines?
just make it a super simple select please

Tested, same error:

that’s not the same error…

1 Like

i’m gonna say something now that is just a guess:

inside the loop, there is another shell
That shell doesn’t have a connection to the database.
try connecting to the database inside the loop (not great i know but just a test)

DB Dump

--
-- PostgreSQL database dump
--

-- Dumped from database version 12.9 (Ubuntu 12.9-2.pgdg20.04+1)
-- Dumped by pg_dump version 12.9 (Ubuntu 12.9-2.pgdg20.04+1)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

DROP DATABASE number_guessing_game;
--
-- Name: number_guessing_game; Type: DATABASE; Schema: -; Owner: freecodecamp
--

CREATE DATABASE number_guessing_game WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8';


ALTER DATABASE number_guessing_game OWNER TO freecodecamp;

\connect number_guessing_game

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: guessing_game; Type: TABLE; Schema: public; Owner: freecodecamp
--

CREATE TABLE public.guessing_game (
    game_id integer NOT NULL,
    username character varying(22) NOT NULL,
    score integer NOT NULL
);


ALTER TABLE public.guessing_game OWNER TO freecodecamp;

--
-- Name: guessing_game_game_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--

CREATE SEQUENCE public.guessing_game_game_id_seq
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


ALTER TABLE public.guessing_game_game_id_seq OWNER TO freecodecamp;

--
-- Name: guessing_game_game_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--

ALTER SEQUENCE public.guessing_game_game_id_seq OWNED BY public.guessing_game.game_id;


--
-- Name: guessing_game game_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--

ALTER TABLE ONLY public.guessing_game ALTER COLUMN game_id SET DEFAULT nextval('public.guessing_game_game_id_seq'::regclass);


--
-- Data for Name: guessing_game; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--

INSERT INTO public.guessing_game VALUES (1, 'Test1', 15);
INSERT INTO public.guessing_game VALUES (2, 'Test2', 6);
INSERT INTO public.guessing_game VALUES (3, 'Test1', 12);
INSERT INTO public.guessing_game VALUES (4, 'Test2', 19);


--
-- Name: guessing_game_game_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--

SELECT pg_catalog.setval('public.guessing_game_game_id_seq', 4, true);


--
-- PostgreSQL database dump complete
--


Script:

#!/bin/bash
# Number Guessing Game

PSQL="psql -X --username=freecodecamp --dbname=number_guessing_game -t --no-align -c" 

# functions
# main guess
MAIN_GUESS() {

read GUESS

if [[ $GUESS =~ ^[0-9]*$ ]]
then

echo "Main guess: $GUESS"
GAME_LOOP $GUESS $SECRET_NUMBER $NUMBER_OF_TRIES $PSQL

else
echo "That is not an integer, guess again:"
MAIN_GUESS
fi
}

# loop function
GAME_LOOP() {
  GUESS=$1
  SECRET_NUMBER=$2
  NUMBER_OF_TRIES=$3
  PSQL=$4

  ((NUMBER_OF_TRIES++))
  echo -e "\nLoop guess: $GUESS\nLoop answer: $SECRET_NUMBER\nLoop tries: $NUMBER_OF_TRIES"
  
  # correct pass
  if [[ $GUESS == $SECRET_NUMBER ]]
  then

  echo $($PSQL "SELECT * FROM guessing_game")
  echo "You guessed it in $NUMBER_OF_TRIES tries. The secret number was $SECRET_NUMBER. Nice job!"
  
  else
  if [[ $GUESS > $SECRET_NUMBER ]]
  then
  echo "It's lower than that, guess again:"
  MAIN_GUESS $GUESS $SECRET_NUMBER $NUMBER_OF_TRIES

  else
  if [[ $GUESS < $SECRET_NUMBER ]]
  then
  echo "It's higher than that, guess again:"
  MAIN_GUESS $GUESS $SECRET_NUMBER $NUMBER_OF_TRIES
  fi
  fi
  fi
}

# start of game test-mode

NUMBER_OF_TRIES=0
echo "test: Number of tries: $NUMBER_OF_TRIES"
SECRET_NUMBER=$(( RANDOM % 1000 +1))
echo "test: Answer is $SECRET_NUMBER"

echo -e "\n~~~~~ Number Guessing Game ~~~~~\n"

# get username
echo "Enter your username:"
read USERNAME

USERNAME_RESULT=$($PSQL "SELECT username FROM guessing_game WHERE username = '$USERNAME'")

# if no username in database
if [[ -z $USERNAME_RESULT ]]
then

# welcome message
echo "Welcome, $USERNAME! It looks like this is your first time here."

else
echo "Welcome back, $USERNAME!"
fi

echo "Guess the secret number between 1 and 1000:"

MAIN_GUESS

this is probably a sub-shell which is why the psql can’t connect…

I’ve passed the $PSQL variable into the loop as an argument within the code