skip to Main Content

The EXECUTE statement does not recognize upper case letters on the database name.

Running the below query on a database named "ABcdeFG"

DO
$c$
BEGIN
EXECUTE 'ALTER DATABASE ' || current_database() || ' SET client_encoding = $$LATIN1$$';
END;
$c$;

returns the following error:

"ERROR:  database "abcdefg" does not exist
CONTEXT:  SQL statement "ALTER DATABASE ABcdeFG SET client_encoding = $$LATIN1$$"
PL/pgSQL function inline_code_block line 3 at EXECUTE

********** Error **********

ERROR: database "abcdefg" does not exist
SQL state: 3D000
Context: SQL statement "ALTER DATABASE ABcdeFG SET client_encoding = $$LATIN1$$"
PL/pgSQL function inline_code_block line 3 at EXECUTE"

Is there a way for this command to be executed considering the upper and lower case letters in the database name, as informed?

2

Answers


  1. Chosen as BEST ANSWER

    It worked by puting the database name in quotes:

    DO
    $c$
    BEGIN
    EXECUTE 'ALTER DATABASE "' || current_database() || '" SET client_encoding = $$LATIN1$$';
    END;
    $c$;
    

  2. You can use the quote_ident function to quote the database name, which will preserve the case.

    DO
    $c$
    BEGIN
    EXECUTE 'ALTER DATABASE ' || quote_ident(current_database()) || ' SET 
    client_encoding = $$LATIN1$$';
    END;
    $c$;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search