Cannot simply use PostgreSQL table name (“relation does not exist”)

后端 未结 11 2639
一向
一向 2020-11-22 05:35

I\'m trying to run the following PHP script to do a simple database query:

$db_host = \"localhost\";
$db_name = \"showfinder\";
$username = \"user\";
$passwo         


        
相关标签:
11条回答
  • 2020-11-22 06:05

    You must write schema name and table name in qutotation mark. As below:

    select * from "schemaName"."tableName";
    
    0 讨论(0)
  • 2020-11-22 06:10

    This is realy helpfull

    SET search_path TO schema,public;
    

    I digged this issues more, and found out about how to set this "search_path" by defoult for a new user in current database.

    Open DataBase Properties then open Sheet "Variables" and simply add this variable for your user with actual value.

    So now your user will get this schema_name by defoult and you could use tableName without schemaName.

    0 讨论(0)
  • 2020-11-22 06:15

    Postgres process query different from other RDMS. Put schema name in double quote before your table name like this, "SCHEMA_NAME"."SF_Bands"

    0 讨论(0)
  • 2020-11-22 06:19

    From what I've read, this error means that you're not referencing the table name correctly. One common reason is that the table is defined with a mixed-case spelling, and you're trying to query it with all lower-case.

    In other words, the following fails:

    CREATE TABLE "SF_Bands" ( ... );
    
    SELECT * FROM sf_bands;  -- ERROR!
    

    Use double-quotes to delimit identifiers so you can use the specific mixed-case spelling as the table is defined.

    SELECT * FROM "SF_Bands";
    

    Re your comment, you can add a schema to the "search_path" so that when you reference a table name without qualifying its schema, the query will match that table name by checked each schema in order. Just like PATH in the shell or include_path in PHP, etc. You can check your current schema search path:

    SHOW search_path
      "$user",public
    

    You can change your schema search path:

    SET search_path TO showfinder,public;
    

    See also http://www.postgresql.org/docs/8.3/static/ddl-schemas.html

    0 讨论(0)
  • 2020-11-22 06:19

    For me the problem was, that I had used a query to that particular table while Django was initialized. Of course it will then throw an error, because those tables did not exist. In my case, it was a get_or_create method within a admin.py file, that was executed whenever the software ran any kind of operation (in this case the migration). Hope that helps someone.

    0 讨论(0)
提交回复
热议问题