# Postgres non-public schema support

**URL:** <https://discourse.diesel.rs/t/postgres-non-public-schema-support/204>\
**Category:** Uncategorized\
**Created:** [November 29, 2020, 3:23pm UTC](https://discourse.diesel.rs/t/postgres-non-public-schema-support/204 "2020-11-29T15:23:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric\_opines](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/eric_opines/32/101_2.png) [@eric\_opines](https://discourse.diesel.rs/u/eric_opines)\
**Post date:** [November 29, 2020, 3:23pm UTC](https://discourse.diesel.rs/t/postgres-non-public-schema-support/204/1 "2020-11-29T15:23:29Z")

</div>

I’m using Diesel 1.4.5 and Diesel CLI 1.4.1.

I’ve been searching for a way to use a schema other than public in Postgres via the Diesel.

I’ve tried various ways and the closest I’ve come is to create the Postgres schema through migrations but, even though the migrations succeed, schema.rs isn’t generated.

Are non-public Postgres schemas not supported?

---

<div class="post-metadata">

**Author:** ![weiznich](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/weiznich/32/64_2.png) [@weiznich](https://discourse.diesel.rs/u/weiznich)\
**Post date:** [November 30, 2020, 10:02am UTC](https://discourse.diesel.rs/t/postgres-non-public-schema-support/204/2 "2020-11-30T10:02:10Z")

</div>

For any schema other then public you need to configure for which schema `schema.rs` should be generated. See the documentation [here](http://diesel.rs/guides/configuring-diesel-cli/) for details.

---

<div class="post-metadata">

**Author:** ![eric\_opines](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/eric_opines/32/101_2.png) [@eric\_opines](https://discourse.diesel.rs/u/eric_opines)\
**Post date:** [December 1, 2020, 1:57am UTC](https://discourse.diesel.rs/t/postgres-non-public-schema-support/204/3 "2020-12-01T01:57:32Z")

</div>

@weiznich  
That was one of the things I tried.

I added schema = “some\_schema” to my diesel.toml file and ran “diesel migration run”. schema.rs was update with

```
pub mod some_schema {}

```

and the tables were created in public again.

So, maybe I’m misunderstanding something about those docs?

---

<div class="post-metadata">

**Author:** ![weiznich](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/weiznich/32/64_2.png) [@weiznich](https://discourse.diesel.rs/u/weiznich)\
**Post date:** [December 1, 2020, 10:04am UTC](https://discourse.diesel.rs/t/postgres-non-public-schema-support/204/4 "2020-12-01T10:04:18Z")

</div>

Maybe you can provide a list of steps to reproduce your issue, otherwise I fear it’s not possible to give another answer than: It’s supported but requires some setup.

---

<div class="post-metadata">

**Author:** ![eric\_opines](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/eric_opines/32/101_2.png) [@eric\_opines](https://discourse.diesel.rs/u/eric_opines)\
**Post date:** [December 3, 2020, 2:12am UTC](https://discourse.diesel.rs/t/postgres-non-public-schema-support/204/5 "2020-12-03T02:12:47Z")

</div>

I’ll see if I can duplicate it in a separate project and push it to github if I can.

---

<div class="post-metadata">

**Author:** ![eric\_opines](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/eric_opines/32/101_2.png) [@eric\_opines](https://discourse.diesel.rs/u/eric_opines)\
**Post date:** [December 6, 2020, 7:18pm UTC](https://discourse.diesel.rs/t/postgres-non-public-schema-support/204/6 "2020-12-06T19:18:44Z")

</div>

See the following repo for a project demonstrating the problem.

> **[ereichert/dieselschematest](https://github.com/ereichert/dieselschematest)**
>
> Demo of possible Diesel CLI bug. Contribute to ereichert/dieselschematest development by creating an account on GitHub.

I’ve added the schema field to diesel.toml.

[https://github.com/ereichert/dieselschematest/blob/main/diesel.toml#L6](https://github.com/ereichert/dieselschematest/blob/main/diesel.toml#L6)

When I run

```
==> diesel migration run

```

the tables are created in the public schema in the dieselschematest database.

![Screen Shot 2020-12-06 at 2.14.49 PM](https://canada1.discourse-cdn.com/flex036/uploads/diesel/original/1X/c7a9865778e456f77dec6ceac8a685e49c0f40a6.png)

In addition, the schema isn’t written to src/schema.rs.

If I comment out the schema field in diesel.toml everything works as expected using the public schema.

I’m happy to provide more information.

---

<div class="post-metadata">

**Author:** ![weiznich](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/weiznich/32/64_2.png) [@weiznich](https://discourse.diesel.rs/u/weiznich)\
**Post date:** [December 7, 2020, 9:52am UTC](https://discourse.diesel.rs/t/postgres-non-public-schema-support/204/7 "2020-12-07T09:52:13Z")

</div>

Ok that seems easy to answer. To make this easier to follow I will copy the relevant parts of your project into my post.

You migration looks like this:

```sql
CREATE TABLE somethings
(
    st_id SERIAL PRIMARY KEY,
    fielda TEXT NOT NULL,
    fieldb TEXT NOT NULL
);

```

According to the postgres documentation this creates a table in the default schema (which is `public` not `somethingsschema`).

If you want to create a table in a custom schema you need to explicitly create that schema first as part of your migrations and then create the table inside of this schema. A corresponding schema would look like this:

```sql
CREATE SCHEMA somethingsschema;

CREATE TABLE somethingsschema.somethings
(
    st_id SERIAL PRIMARY KEY,
    fielda TEXT NOT NULL,
    fieldb TEXT NOT NULL
);

```

---

<div class="post-metadata">

**Author:** ![eric\_opines](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/eric_opines/32/101_2.png) [@eric\_opines](https://discourse.diesel.rs/u/eric_opines)\
**Post date:** [December 7, 2020, 4:10pm UTC](https://discourse.diesel.rs/t/postgres-non-public-schema-support/204/8 "2020-12-07T16:10:55Z")

</div>

This is interesting. When I was troubleshooting this issue last week I tried that in the real project I’m working on. I tried it again in the real project this morning. It does not work.

I tried with the example project and it works.

Should I delete all of the Diesel files in the real project and try ‘diesel setup’? Or is there something else I should investigate first?

---

<div class="post-metadata">

**Author:** ![weiznich](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/weiznich/32/64_2.png) [@weiznich](https://discourse.diesel.rs/u/weiznich)\
**Post date:** [December 8, 2020, 1:26pm UTC](https://discourse.diesel.rs/t/postgres-non-public-schema-support/204/9 "2020-12-08T13:26:38Z")

</div>

> [@eric\_opines](#):
>
> Should I delete all of the Diesel files in the real project and try ‘diesel setup’? Or is there something else I should investigate first?

It shouldn’t be necessary to delete any diesel files here. The only thing you need to check if you’ve already applied your migrations and changed them (inside of your migration directory) afterwards. If that’s the case just delete and recreate the database.

---

<div class="post-metadata">

**Author:** ![eric\_opines](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/eric_opines/32/101_2.png) [@eric\_opines](https://discourse.diesel.rs/u/eric_opines)\
**Post date:** [December 12, 2020, 8:45pm UTC](https://discourse.diesel.rs/t/postgres-non-public-schema-support/204/10 "2020-12-12T20:45:05Z")

</div>

I just figured this out.

One has to include the correct SQL AND the correct config for everything to work correctly.

With the correct migrations defined as @weiznich indicated above, the correct schema will be generated in the database. However, without adding the `schema = “some_schema”`in the `./diesel.toml` file, the `./src/schema.rs` will not be generated properly.

Should the docs be updated to reflect this?

---

<div class="post-metadata">

**Author:** ![weiznich](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.diesel.rs/weiznich/32/64_2.png) [@weiznich](https://discourse.diesel.rs/u/weiznich)\
**Post date:** [December 14, 2020, 8:48am UTC](https://discourse.diesel.rs/t/postgres-non-public-schema-support/204/11 "2020-12-14T08:48:15Z")

</div>

> [@eric\_opines](#):
>
> Should the docs be updated to reflect this?

It’s not clear what documentation should be updated to reflect this. I mean the diesel cli documentation already mentions the `schema` flag/key in various places. Otherwise this is just normal SQL for postgresql, which is documented in their documentation.
