Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgres: relation does not exist when the table clearly exists when calling from node.js

so I psql'd and created table users;

 CREATE TABLE users (
    id integer NOT NULL,
    username text
);

I am able to grab rows by doing SELECT * FROM users;

However, when I use node.js with the library module pg to make calls I get the infamous relation does not exist.

const createQuery = {text: "INSERT INTO users(id, username) VALUES($1)",values: values}
const { rows } = await db.query(createQuery);

I wasn't running into this issue before a complete server migration.

like image 924
user299709 Avatar asked Dec 01 '25 14:12

user299709


1 Answers

There can two possible reasons for this situation:

  1. You have created this relation in a different schema. PostgreSQL allows you to organize your relations into different namespaces called schemas. To specify a schema CREATE TABLE myschema.users (...), similarly, CREATE TABLE myschema.users. If the schema name is not specified, PostgreSql will use the defualt schema, usually it's public. To check your which schema does your relation belongs to run this query, SELECT table_schema, table_name FROM information_schema.tables WHERE table_name = 'users'.
  2. You can check which database and host you are connected to by using the SELECT current_database() and SELECT inet_server_addr() queries, just in case if you have connected to wrong database/host.
like image 113
Hassan Rehan Avatar answered Dec 03 '25 04:12

Hassan Rehan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!