# Best way to start PG (Process Groups)

**URL:** <https://erlangforums.com/t/best-way-to-start-pg-process-groups/1545>\
**Category:** Questions / Help\
**Tags:** rebar3, process-groups\
**Created:** [June 15, 2022, 10:26am UTC](https://erlangforums.com/t/best-way-to-start-pg-process-groups/1545 "2022-06-15T10:26:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ziopio](https://erlangforums.com/user_avatar/erlangforums.com/ziopio/32/1498_2.png) [@ziopio](https://erlangforums.com/u/ziopio)\
**Post date:** [June 15, 2022, 10:26am UTC](https://erlangforums.com/t/best-way-to-start-pg-process-groups/1545/1 "2022-06-15T10:26:37Z")

</div>

What is the best way of starting `pg` ?

- Manually calling `pg:start_link()` ?
- Inside the top supervisor?
- Through kernel config by setting `{start_pg, true}` ?

Problem by using `{start_pg, true}` :

```erlang
{shell, [
    {apps, [my_app]},
    {config, "./config/my.config"}
]}.

```

Inside my.config i have:  
`{kernel, [{start_pg, true}]},`  
I noticed that if I start `pg` with kernel config, `rebar3 shell` does not have `pg` running.  
Although i can clearly match `true = application:get_env(kernel, start_pg).`  
If i release with the same config i have `pg` running, so the option works.

Is rebar3 already starting kernel before evaluating the config ?  
If so, why does it handle stuff like logger configurations and not other settings as start\_pg?  
Ultimately, how can i have pg running both in shell and in release in the most clean way?

---

<div class="post-metadata">

**Author:** ![filmor](https://erlangforums.com/user_avatar/erlangforums.com/filmor/32/988_2.png) [@filmor](https://erlangforums.com/u/filmor)\
**Post date:** [June 15, 2022, 2:37pm UTC](https://erlangforums.com/t/best-way-to-start-pg-process-groups/1545/2 "2022-06-15T14:37:46Z")

</div>

I decided that I didn’t want a “globally” shared scope for my process groups (I use them mainly for pub/sub right now), so I just start them in my own supervisor as

```erlang
#{
    id => my_pg,
    start => {pg, start_link, [my_pg_scope]}
}

```

---

<div class="post-metadata">

**Author:** ![ziopio](https://erlangforums.com/user_avatar/erlangforums.com/ziopio/32/1498_2.png) [@ziopio](https://erlangforums.com/u/ziopio)\
**Post date:** [June 15, 2022, 7:43pm UTC](https://erlangforums.com/t/best-way-to-start-pg-process-groups/1545/3 "2022-06-15T19:43:32Z")

</div>

I ended up with this solution since it seams to be the best option. Thank you!

---

<div class="post-metadata">

**Author:** ![max-au](https://erlangforums.com/user_avatar/erlangforums.com/max-au/32/1627_2.png) [@max-au](https://erlangforums.com/u/max-au)\
**Post date:** [June 17, 2022, 4:34pm UTC](https://erlangforums.com/t/best-way-to-start-pg-process-groups/1545/4 "2022-06-17T16:34:48Z")

</div>

> [@ziopio](#):
>
> Is rebar3 already starting kernel before evaluating the config ?

Yes. You can run `ERL_FLAGS="-kernel start_pg true" rebar3 shell` if you want the default scope supervised by the kernel.

My recommendation would be to have a scope supervised by your own application supervisor (this answer is already marked). That’s how `pg` was designed to work anyway. The default scope was mainly done for a simplified migration from `pg2`.
