6


I'm trying to connect to my postgres database.

Everything is working fine, but in my database I've got 2 schemas. One of them is created by default by PgAdmin (public), and the second one, ZTB, which I have created.

When I connect to my database using NodeJS, it by default connects to the "public" schema, and therefore I cannot query tables from "ZTB".

Is there any solution to switch the current working schema in the NodeJS server?

Regards

4
  • Welcome to stackoverflow, Rafael. Here's your guide on How to create a Minimal, Complete, and Verifiable example. Can you tell us which PG module you are using and the connection string you are forming? Relevant snippets of your implementation will help us help you. Commented Jan 27, 2018 at 12:14
  • 3
    set search_path ... postgresql.org/docs/current/static/… Commented Jan 27, 2018 at 12:22
  • I resolved it myself. client.query("SET search_path TO 'ZTB';"); did the job. Update: exactly as @a_horse_with_no_name said. Commented Jan 27, 2018 at 12:23
  • @Rafael can you post your comment as an answer? Commented Nov 12, 2018 at 12:57

3 Answers 3

1

I found solution myself - client.query("SET search_path TO 'ZTB';"); did the job exactly as @a_horse_with_no_name said later.

Sign up to request clarification or add additional context in comments.

Comments

0

First of all, change your schema name to lower case. Having upper case may create problems.

Next, whatever you want to query just do [your schema name].[your table name], and you should be fine.

client.query("select * from ztb.[your table name]"))

1 Comment

Thanks for response but this is not an answer for my question and it was resolved almost 2 years ago. ;)
0

If you using pg-promise, then choosing the schema is available right out of the box.

Within Initialization Options, there is option schema that supports flexible values:

const pgp = require('pg-promise')({
    schema: ['public', 'ZTB'] // access tables from "public" + "ZTB"
});

And you can set schema even to a function that returns the schema(s).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.