1

I have a database created through Postgresql. I'm attempting to connect to the database via Knex.js.

In my text editor (atom) I have required knex

const knex = require('knex');

Second - I have begun to initialize the library like so:

const postgres = knex ({ client: 'pg', connection: { host: '127.0.0.1', port: '5432', user: 'is myself', password: '', database: 'mediumrare_database' } });

Third - I am attempting to pull data from that db like so:

postgres.select('*').from('mediumrare_database).then(data => {console.log(data)});

Finally, the error message I am receiving is as follows:

Unhandled rejection error: relation "mediumrare_database" does not exist

alxdelgado
  • 37
  • 9
  • 1
    I dont think its related but your query lacks a single-quote at the end `postgres.select('*').from('mediumrare_database')` – Rashomon May 14 '19 at 19:04
  • 1
    Hi there, welcome to SO ! If after fixing what @Rashomon suggested (really might be the problem), the error persist, I suggest you check this: https://stackoverflow.com/questions/695289/cannot-simply-use-postgresql-table-name-relation-does-not-exist – Pierre C. May 14 '19 at 19:08
  • Possible duplicate of [Cannot simply use PostgreSQL table name ("relation does not exist")](https://stackoverflow.com/questions/695289/cannot-simply-use-postgresql-table-name-relation-does-not-exist) – Pierre C. May 14 '19 at 19:08
  • 1
    @Rashomon do you mean like this ```postgres.select('*').from('mediumrare_database')' ``` – alxdelgado May 14 '19 at 19:28
  • 1
    @SherloxFR I did read through the other forum you posted, but that was dealing with the lowercase/uppercase naming convention. I've been able to start 'psql' database from the terminal fine. – alxdelgado May 14 '19 at 19:31
  • @alxdelgado yeah that's what he means. Can you run the `SELECT * FROM mediumrare_database;` command in `psql` after using `USE mediumrare_database`. – Pierre C. May 14 '19 at 19:34
  • @SherloxFR. Ok, it's a little weird. When creating my database I named it 'mediumrare_database' (this is what also shows up in the postgres GUI. However the name of the table is called 'vinyl_information'. When I do ```SELECT * FROM vinyl_information;``` I get all of my records. When I run ```SELECT * FROM mediumrare_database;``` I get the error - relation does not exist. In my code I've tried to substitute ```vinyl_information``` for ```mediumrare_database``` and I am still getting the same error. To be more specific, it only happens when I use .then(). – alxdelgado May 14 '19 at 19:39

1 Answers1

0

Your database is named mediumrare_database, and the table you are trying to get datas from is named the same way.

So it seems the problem is that you didn't created any table.

In SQL, your Knex commands would be: SELECT * FROM mediumrare_database;, which means Return all the datas inside the 'mediumrare_database' table.

You first need to create your database structure (tables to store data) with a CREATE instruction. See https://knexjs.org/#Schema-createTable.

FROM is meant to be used on a table, as you already specified the database to connect to in the connection string.

I can only suggest you learn the basics of SQL before using Knex, which is an SQL Query Builder. You will run into a lot of problems if you don't understand the underlying system and language, because Knex will not teach you that :)

You can check the one of CodeCademy, they always have great ressources.

EDIT: So you created a vinyl_information table (comments on OP). Your Knex command should then be:

postgres
  .select('*')
  .from('vinyl_information')
  .then(data => console.log(data))

I have a Discord server where we help each others, don't hesitate joining it if you need further help on the topic :) https://discord.gg/C2bVzgb

Pierre C.
  • 1,591
  • 2
  • 16
  • 29
  • Thank you for the help first & foremost. I do actually have the code you wrote. ```postgres.select('*').from('mediumrare_database').then(data => { console.log(data); });``` – alxdelgado May 14 '19 at 19:48
  • 1
    You are a god! it works, thank you so much. I'll definitely dive into that SQL course. – alxdelgado May 14 '19 at 19:53
  • @alxdelgado I suggest you join the Discord with the link I posted, I will be able to help you far more quickly and simply there for those kind of "simple" requests :) Could you please Accept and Upvote my answer to close your question ? – Pierre C. May 14 '19 at 19:55