Skip to content

Specify Postgres schema #2635

Answered by andrewmbenton
rickb777 asked this question in Q&A
Aug 18, 2023 · 4 comments · 3 replies
Discussion options

You must be logged in to vote

Unfortunately there is no way to parameterize a schema name in a query (see this SO thread).

But you can pass a schema name as a connection parameter, which will set the search_path. So presuming you have some way of setting up a separate schema per tenant, you can scope access to that schema at the connection level.

If you are trying to avoid separate connection pools per tenant, the only thing I can think of is to run SET SCHEMA 'tenant_x_schema_name'; before every query. But I haven't actually tried that and it seems error prone. (EDIT: I don't think this is safe, see @rickb777's comment below.)

In theory sqlc could probably generate code which does that (based on a configuration), but…

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@andrewmbenton
Comment options

Comment options

You must be logged in to vote
1 reply
@andrewmbenton
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by andrewmbenton
Comment options

You must be logged in to vote
1 reply
@andrewmbenton
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
enhancement New feature or request
2 participants
Converted from issue

This discussion was converted from issue #2633 on August 18, 2023 22:05.