Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do you fix relation does not exist?

When you encounter this error, the first thing to check is to make sure that the Sequelize code points to the right table name. This error can also occur in your migration code because you might have migration files that create a relationship between two tables.

What is a relation in Postgres?

PostgreSQL is a relational database management system ( RDBMS ). That means it is a system for managing data stored in relations. Relation is essentially a mathematical term for table.

Is Postgres case sensitive?

PostgreSQL names are case sensitive. By default, AWS Schema Conversion Tool (AWS SCT) uses object name in lowercase for PostgreSQL. In most cases, you'll want to use AWS Database Migration Service transformations to change schema, table, and column names to lower case.

How do I list all tables in PostgreSQL?

To list the tables in the current database, you can run the \dt command, in psql : If you want to perform an SQL query instead, run this: SELECT table_name FROM information_schema.


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


I had problems with this and this is the story (sad but true) :

  1. If your table name is all lower case like : accounts you can use: select * from AcCounTs and it will work fine

  2. If your table name is all lower case like : accounts The following will fail: select * from "AcCounTs"

  3. If your table name is mixed case like : Accounts The following will fail: select * from accounts

  4. If your table name is mixed case like : Accounts The following will work OK: select * from "Accounts"

I dont like remembering useless stuff like this but you have to ;)


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


Put the dbname parameter in your connection string. It works for me while everything else failed.

Also when doing the select, specify the your_schema.your_table like this:

select * from my_schema.your_table

I had a similar problem on OSX but tried to play around with double and single quotes. For your case, you could try something like this

$query = 'SELECT * FROM "sf_bands"'; // NOTE: double quotes on "sf_Bands"

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

select * from "schemaName"."tableName";