javascript - How to set schema in pg-promise - Stack Overflow

I was searching the documentation of pg-promise specifically in the creation of the client. But I wasn&

I was searching the documentation of pg-promise specifically in the creation of the client. But I wasn't able to find the option to set the default schema to be used in the connection, it always uses public schema. How do I set it?

I was searching the documentation of pg-promise specifically in the creation of the client. But I wasn't able to find the option to set the default schema to be used in the connection, it always uses public schema. How do I set it?

Share edited Apr 17, 2018 at 16:15 vitaly-t 26k17 gold badges127 silver badges150 bronze badges asked Jan 18, 2017 at 22:58 Emmanuel CamposEmmanuel Campos 7263 gold badges9 silver badges24 bronze badges 5
  • 1 Default schema is controlled by the server, not by the client, i.e. the library doesn't use public by default, it uses whatever is set as the default on the server. – vitaly-t Commented Jan 18, 2017 at 23:13
  • Also see this: stackoverflow./questions/2875610/… – vitaly-t Commented Jan 18, 2017 at 23:20
  • 2 You can set the schema search path at the user level: see answer http://stackoverflow./questions/2951875/postgresql-how-do-i-set-the-search-path-at-the-user-level – Tim Child Commented Jan 18, 2017 at 23:27
  • Ah that's why it's not in the connection parameters. Ok thank you. The link you sent me was really helpful also.So my solution was to set the schema on server start using the SET search_path query. – Emmanuel Campos Commented Jan 18, 2017 at 23:28
  • Alternatively, you can have the library inject the schema name automatically. See pg-promise-demo. – vitaly-t Commented Jan 18, 2017 at 23:32
Add a ment  | 

1 Answer 1

Reset to default 8

Normally, one sets the default schema(s) for the database or the role, as explained here:

  • Permanently Set Postgresql Schema Path

It is only if you want to do so without persisting the change, you might want to set the schema(s) dynamically, just for the current process.

The library supports option schema within Initialization Options:

const initOptions = {
    schema: 'my_schema' /* can also be an array of strings or a callback */
};

const pgp = require('pg-promise')(initOptions);

making it easier to set the dynamic schema(s).

Examples

  • Making your own schema visible along with the default public schema:

    const initOptions = {
        schema: ['public', 'my_schema'] /* make both schemas visible */
    };
    
    const pgp = require('pg-promise')(initOptions);
    
  • Using the callback to set schema based on the Database Context (see Database constructor):

    const initOptions = {
        schema(dc) {
            if(dc === /* whatever Database Context was used */) {
                return 'my_schema'; /* or an array of strings */
            }
            /* other provisions, if multiple databases are used. */
    
            /* can return null/undefined, if no schema change is needed. */
        }
    };
    
    const pgp = require('pg-promise')(initOptions);
    

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742290980a4416181.html

相关推荐

  • javascript - How to set schema in pg-promise - Stack Overflow

    I was searching the documentation of pg-promise specifically in the creation of the client. But I wasn&

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信