Kinnard Hockenhull
Kinnard Hockenhull

Reputation: 3010

Error Relation does not exist

I am getting a [error: relation "causes" does not exist] error from my node app. The relation does exist, I'm not sure what the problem is.

I created the table with

CREATE TABLE causes (

cause_id bigint NOT NULL default nextval('causes_cause_id_seq'::regclass),
cause_name varchar(40) NOT NULL,
goal integer,
sponsor varchar(30),
organization varchar(30),
submitter varchar(30),
address varchar(34),
balance numeric

);

This is the query that's giving the error:

client = pg.connect(connectionString, function(err, client, done){
    if(err) console.log(err);

    client.query('INSERT INTO causes (cause_name, goal, organization, sponsor, submitter) VALUES ($1,$2,$3,$4,$5) RETURNING *', r, function(err, result){
    if(err) console.log(err);
    });
});

Upvotes: 14

Views: 28419

Answers (8)

Binh Ho
Binh Ho

Reputation: 4934

Make sure 2 things

  • public keyword
  • Quote "

NOT:

SELECT * FROM public.SampleTable
SELECT * FROM SampleTable

SHOULD:

SELECT * FROM public."SampleTable"

Upvotes: 1

keytrap
keytrap

Reputation: 480

I resolved my issue but setting my schema name before table name :

SELECT * FROM public.mytable

instead of

SELECT * FROM mytable

Upvotes: 0

sudo
sudo

Reputation: 5784

If you're connecting using a Postgres URL, specify the database explicitly in it. Sometimes if you don't, whatever lib you're using defaults to your username as the database, which may be the wrong one. I just had this happen to me. See PostgreSQL Connection URL

Upvotes: 1

Weijing Jay Lin
Weijing Jay Lin

Reputation: 3238

I'm not sure if any of you facing the similar situation like I did.

Please make sure you are using correct user/password to the correct database host.

I found I connected to a wrong database :(

Upvotes: 5

kukis
kukis

Reputation: 4634

I had the same problem. I was querying newly created table Example. This was my code:

const { Pool, Client } = require('pg');
const dbClient = new Client({
  user: 'postgres',
  host: 'localhost',
  database: 'postgres',
  password: 'test',
  port: 5432,
});
dbClient.connect();
dbClient.query('SELECT * from Example', (err, res) => {
    console.log(err, res);
    dbClient.end();
});

I double checked connection parameters and any possible typos. It turned out that in pg you need to wrap the table name in quotes:

dbClient.query('SELECT * from "Example"', (err, res) => {
    console.log(err, res);
    dbClient.end();
});

Upvotes: 1

CHNL
CHNL

Reputation: 1

I had a similar error when trying to create a table with user input for table name and then insert into database.

The solution I found was to create the create table and insert into as two separate functions, call the functions back to back, but the second one with a delay.

Guiding code is below:

function createTable(){
// your create table query here
}

function pushData(){
// your insert into table query here
}

createTable()
setTimeout(postTable, 3000); // for 3 seconds, can make is less, I used 3 secs to be safe

Upvotes: -1

chejazi
chejazi

Reputation: 56

Directly before your client.query('INSERT...') call, run the following to ensure that your relation is accessible on the current connection:

client.query('SELECT * FROM pg_catalog.pg_tables', function(err, result) {
  console.log(result);
});

If you don't see your causes relation among the results, then either the relation doesn't exist, or it was created under a different user.

Upvotes: 3

Craig Ringer
Craig Ringer

Reputation: 324455

This is probably a case folding issue. See this answer and the PostgreSQL documentation on SQL syntax.

After edit: Looks like it isn't a case folding issue. Check search_path (SHOW search_path or SELECT current_setting('search_path')) and compare it to the schema the table is in (\dt+ tablename) to make sure the table is on the client's path.

Also make sure you're connecting to the same database.

Upvotes: 2

Related Questions