# Can I run a cron job as user Postgres?

**URL:** https://meta.discourse.org/t/can-i-run-a-cron-job-as-user-postgres/130269
**Category:** Self-hosting
**Created:** [October 4, 2019, 7:05pm UTC](https://meta.discourse.org/t/can-i-run-a-cron-job-as-user-postgres/130269 "2019-10-04T19:05:32Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![slackmoehrle](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/slackmoehrle/32/82215_2.png) [@slackmoehrle](https://meta.discourse.org/u/slackmoehrle)
#### Post date: [October 4, 2019, 7:05pm UTC](https://meta.discourse.org/t/can-i-run-a-cron-job-as-user-postgres/130269/1 "2019-10-04T19:05:32Z")

</div>

I have a number of queries that need to run each night. I thought that I could do the following:

```sh
cd /var/discourse
./launcher enter app
su postgres
crontab -e
... add my tasks and save ...

```

but, I get an error:

```sh
postgres@EN-Discourse-Forums-app:~$ crontab -e
crontab: installing new crontab
crontab: crontabs/postgres: rename: Operation not permitted
crontab: edits left in /tmp/crontab.7kGYwA/crontab

```

Is this possible?

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [October 4, 2019, 7:11pm UTC](https://meta.discourse.org/t/can-i-run-a-cron-job-as-user-postgres/130269/2 "2019-10-04T19:11:54Z")

</div>

The more “Discourse-way” of doing that would be creating a plugin with the queries as scheduled jobs.

Check this example:

> <https://github.com/discourse/discourse-patreon/blob/main/app/jobs/scheduled/patreon_sync_patrons_to_groups.rb>

Inside the `execute` block you can do anything, like this job who runs SQL:

> <https://github.com/discourse/discourse/blob/main/app/jobs/onceoff/clean_up_post_timings.rb>

---

<div class="post-metadata">

### Author: ![slackmoehrle](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/slackmoehrle/32/82215_2.png) [@slackmoehrle](https://meta.discourse.org/u/slackmoehrle)
#### Post date: [October 4, 2019, 7:16pm UTC](https://meta.discourse.org/t/can-i-run-a-cron-job-as-user-postgres/130269/3 "2019-10-04T19:16:25Z")

</div>

Potentially, yes, but this doesn’t let others who may need to change the behavior of these queries and our servers use something they understand. It is very common to use CRON to run queries.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [October 4, 2019, 7:18pm UTC](https://meta.discourse.org/t/can-i-run-a-cron-job-as-user-postgres/130269/4 "2019-10-04T19:18:38Z")

</div>

Did you read [How to add a job to cron/anacron when creating Docker container](https://meta.discourse.org/t/how-to-add-a-job-to-cron-anacron-when-creating-docker-container/123194) ?

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [June 5, 2023, 12:32pm UTC](https://meta.discourse.org/t/can-i-run-a-cron-job-as-user-postgres/130269/5 "2023-06-05T12:32:06Z")

</div>



---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [July 5, 2023, 12:32pm UTC](https://meta.discourse.org/t/can-i-run-a-cron-job-as-user-postgres/130269/6 "2023-07-05T12:32:41Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
