<p>Why are you running <code>cardano-db-sync</code> with cabal? You should just run <code>cardano-db-sync</code> or <code>cardano-db-sync-extended</code>.
And another question, more important: why are you running the service as root? This is always a security issue. You should never run a service as root (unless really needed, which is not the case here) or as an user with sudo rights. And <code>cardano-db-sync</code> should be in <code>/usr/local/bin</code>, for example.</p>
<p>I'm posting here my service file and my shell script executed by the service:</p>
<pre><code># cat /etc/systemd/system/cardano-db-sync.service 
# The Cardano db-sync service (part of systemd)
# file: /etc/systemd/system/cardano-db-sync.service 

[Unit]
Description      = Cardano db-sync service
Wants            = network-online.target
After            = network-online.target 

[Service]
User             = cardano
Type             = simple
WorkingDirectory = /home/cardano/cardano-node
ExecStart        = /bin/bash -c '/home/cardano/cardano-node/bin/db-sync.sh'
KillSignal       = SIGINT
TimeoutStopSec   = 120
LimitNOFILE      = 32768
Restart          = always
RestartSec       = 5

[Install]
WantedBy         = multi-user.target
</code></pre>
<pre><code># cat /home/cardano/cardano-node/bin/db-sync.sh
PGPASSFILE=/home/cardano/cardano-node/config/pgpass-mainnet cardano-db-sync-extended \
    +RTS -N -RTS \
    --config /home/cardano/cardano-node/config/mainnet-db-sync-config.json \
    --socket-path /home/cardano/cardano-node/db/node.socket \
    --state-dir /home/cardano/cardano-node/ledger-state/mainnet \
    --schema-dir /home/cardano/cardano-node/schema/
</code></pre>

CC BY-SA 4.0
2022-03-01T04:15:18.067Z
Hi! Thanks for your explanation and sample codes. It kinda gives me more explanation. About your question "Why are you running `cardano-db-sync` with cabal? You should just run `cardano-db-sync` or `cardano-db-sync-extended` - it seems I kinda misunderstood the [documentation](https://github.com/input-output-hk/cardano-db-sync/blob/master/doc/building-running.md). And, because its last step, I kinda confuse on where can I found the "cardano-db-sync-exe", so, I follow the previous step where it says "... cabal run cardano-db-sync .... "
wandyyd
CC BY-SA 4.0
2022-03-01T04:24:21.403Z
--continue-- About your concern, why I run it as root, it's because the server is a test server, and I still learn things around since I super newbie! Anyway, thanks a lot for that! really means a lot for me to learn! || And "And cardano-db-sync should be in /usr/local/bin" - I don't see that on that folder. I just follow their [docs](https://github.com/input-output-hk/cardano-db-sync/blob/master/doc/building-running.md#run-two-chains-with-a-single-postgresql-instance).
wandyyd
CC BY-SA 4.0
2022-03-01T11:23:57.293Z
You are welcome. I am keeping all cardano-related binaries in /usr/local/bin, it's probably the best location for them.
George - APEX Stake Pool
CC BY-SA 4.0
2022-03-02T02:27:22.187Z
ohh! I see! I thought it supposed to be there after installation. Thanks for your tips!
wandyyd
CC BY-SA 4.0
2022-02-28T17:38:01.383Z
2022-02-28T17:38:01.383Z
George - APEX Stake Pool
<p>Linux/Ubuntu - Cardano newbie, want to ask things around! :)</p>
<p>At the moment, I'm successfully installed cardano-node and can make it run as a service (pulling data automatically on the background), by following some of these <a href="https://www.coincashew.com/coins/overview-ada/guide-how-to-build-a-haskell-stakepool-node#7.-create-startup-scripts" rel="nofollow noreferrer">instruction</a>. I only follow some / few steps.</p>
<p>I tried to replicate it into <code>cardano-db-sync</code> version, so I can do cardano-db-sync automatically on the background. But, it gives some error / issue.</p>
<p>I already make, <code>.sh</code> and <code>.service</code> file.</p>
<p>Below is the code for, <code>.sh</code> file</p>
<pre><code>#!/bin/bash
PGPASSFILE=/cardano/cardano-db-sync/config/pgpass-mainnet
CONFIG=/cardano/cardano-db-sync/config/mainnet-config.yaml
SOCKET_PATH=/cardano/db/node.socket
STATE_DIR=/cardano/cardano-db-sync/ledger-state/mainnet
SCHEMA_DIR=/cardano/cardano-db-sync/schema/
PGPASSFILE=${PGPASSFILE} cabal run cardano-db-sync -- --config ${CONFIG} --socket-path ${CARDANO_NODE_SOCKET_PATH} --state-dir ${STATE_DIR} --schema-dir ${SCHEMA_DIR}
</code></pre>
<p>And for <code>.service</code> file</p>
<pre><code># The Cardano DB Sync service (part of systemd)
# file: /etc/systemd/system/cardano-db-sync-run.service

[Unit]
Description     = Cardano DB Sync service
Wants           = network-online.target
After           = network-online.target

[Service]
User            = root
Type            = simple
WorkingDirectory= /cardano/cardano-db-sync
ExecStart       = /bin/bash -c '/cardano/cardano-db-sync-run.sh'
KillSignal=SIGINT
TimeoutStopSec=300
LimitNOFILE=32768
Restart=always
RestartSec=5
SyslogIdentifier=cardano-db-sync

[Install]
WantedBy        = multi-user.target
</code></pre>
<p>When I test to manually run <code>/bin/bash -c '/cardano/cardano-db-sync-run.sh'</code> it's working well and I can see some data get into database.</p>
<p>BUT, when I tried to run it as a service, it gives and error.
Here's what I did to make it run as a service</p>
<pre><code>&gt; sudo systemctl enable cardano-db-sync-run
&gt; sudo systemctl start cardano-db-sync-run
</code></pre>
<p>And then, checking its log by</p>
<pre><code>&gt; journalctl --unit=cardano-db-sync-run --follow
</code></pre>
<p>And it shows an error - <code>line 7: cabal: command not found</code>.</p>
<p>I tried to see and one of my colleague advise me, to use full path of &quot;cabal&quot;, since it isn't on <code>/bin</code>, so I change a bit <code>.sh</code> file to be</p>
<pre><code>...
...

PGPASSFILE=${PGPASSFILE} /root/.ghcup/bin/cabal run cardano-db-sync -- --config ${CONFIG} --socket-path ${CARDANO_NODE_SOCKET_PATH} --state-dir ${STATE_DIR} --schema-dir ${SCHEMA_DIR}
</code></pre>
<p>Notice, that I change <code>cabal</code> into <code>/root/.ghcup/bin/cabal</code>. But, by doing so, it also gives an error message, as shown below</p>
<pre><code>Feb 28 06:40:44 systemd[1]: Started Cardano DB Sync service.
Feb 28 06:40:44 cardano-db-sync[20548]: /cardano/cardano-db-sync
Feb 28 06:40:45 cardano-db-sync[20548]: Warning: Requested index-state 2021-07-02T00:00:00Z is newer than
Feb 28 06:40:45 cardano-db-sync[20548]: 'hackage.haskell.org'! Falling back to older state (2021-07-01T22:06:28Z).
Feb 28 06:40:45 cardano-db-sync[20548]: Resolving dependencies...
Feb 28 06:40:45 cardano-db-sync[20548]: cabal: Could not resolve dependencies:
Feb 28 06:40:45 cardano-db-sync[20548]: [__0] trying: Win32-network-0.1.0.0 (user goal)
Feb 28 06:40:45 cardano-db-sync[20548]: [__1] trying: base-4.9.1.0/installed-4.9.1.0 (dependency of Win32-network)
Feb 28 06:40:45 cardano-db-sync[20548]: [__2] next goal: base-deriving-via (user goal)
Feb 28 06:40:45 cardano-db-sync[20548]: [__2] rejecting: base-deriving-via-0.1.0.0 (conflict:
Feb 28 06:40:45 cardano-db-sync[20548]: base==4.9.1.0/installed-4.9.1.0, base-deriving-via =&gt; base&gt;=4.14)
Feb 28 06:40:45 cardano-db-sync[20548]: [__2] fail (backjumping, conflict set: base, base-deriving-via)
Feb 28 06:40:45 cardano-db-sync[20548]: After searching the rest of the dependency tree exhaustively, these were the
Feb 28 06:40:45 cardano-db-sync[20548]: goals I've had most trouble fulfilling: base, Win32-network, base-deriving-via
Feb 28 06:40:45 systemd[1]: cardano-db-sync-run.service: Main process exited, code=exited, status=1/FAILURE
Feb 28 06:40:45 systemd[1]: cardano-db-sync-run.service: Failed with result 'exit-code'.
</code></pre>
<hr>
<p>Again, I already try to run it manually, using <code>/bin/bash -c '/cardano/cardano-db-sync-run.sh'</code> and it works well, BUT, only when I make it run as a service, it shows that error.</p>
<p>My guess is that &quot;cabal&quot; isn't really recognized.</p>
<p>Any suggestion on why it failed?</p>
<hr>
<p>PS --</p>
<ol>
<li>I built cardano-node by follow <a href="https://developers.cardano.org/docs/get-started/installing-cardano-node" rel="nofollow noreferrer">this docs</a> precisely. So, even the <code>ghc --version</code> and/or <code>cabal --version</code> is same</li>
<li>cardano-db-sync that I built is using Cabal (ghc stuffs), not using Nix / docker.</li>
</ol>
<hr>
<p>Thanks in advance!</p>
<br>
<hr>
<p>*Edit #1 - after read the comments and replies from <a href="https://cardano.stackexchange.com/users/5926">Eric de Castro Lopo</a> and <a href="https://cardano.stackexchange.com/users/5122/george-apex-stake-pool">George - APEX Stake Pool</a>, I got question</p>
<p>Do I need to wait this part to be done? So, I can make cardano-db-sync as a service?</p>
<blockquote>
<p>PGPASSFILE=config/pgpass-mainnet cabal run cardano-db-sync -- <br />
--config config/mainnet-config.yaml <br />
--socket-path ../cardano-node/state-node-mainnet/node.socket <br />
--state-dir ledger-state/mainnet <br />
--schema-dir schema/</p>
</blockquote>
<p>*It's written on the <a href="https://github.com/input-output-hk/cardano-db-sync/blob/master/doc/building-running.md#set-up-and-run-the-db-sync-node" rel="nofollow noreferrer">docs</a></p>
<br>
<hr>
<p>*Edit #2 - after checking more</p>
<p>Question from *Edit #1 - can be ignored, but for someone who looking for the answer, the answer is didn't need to. Detail can be seen on comment / replies by others.</p>
<p>Anyway, finally I can make it work as service. And as per guidance from <a href="https://cardano.stackexchange.com/users/5926">Eric de Castro Lopo</a> and <a href="https://cardano.stackexchange.com/users/5122/george-apex-stake-pool">George - APEX Stake Pool</a>, seems I mistakenly put the syntax/command on <code>.sh</code> file as mentioned. Thanks!</p>
<p>In-case someone need, the workaround I did is to make a symlink of the <code>executable</code> file of <code>cardano-db-sync</code></p>
<ol>
<li>Do <code>find . -name cardano-db-sync -executable -type f</code> to find its location</li>
<li>Then, I did symlink (register <code>cardano-db-sync</code>) into /usr/local/bin (or wherever suitable for you), using <code>sudo ln -s  ...</code> (or other similar syntax / command).</li>
<li>And finally, on <code>.sh</code> file, I change this part <code>... cabal run cardano-db-sync -- ...</code> into <code>... cardano-db-sync -- ...</code></li>
</ol>
<hr>
<p>Anyway, I will mark <a href="https://cardano.stackexchange.com/users/5122/george-apex-stake-pool">George - APEX Stake Pool</a>'s answer as it's the closest hint for me.</p>
<p>And, sorry <a href="https://cardano.stackexchange.com/users/5926">Eric de Castro Lopo</a> that I couldn't upvote your comment, as it's helpful too for me! Need +15 rep to upvote your comment :(</p>
<p>Thanks for your time! :)</p>

CC BY-SA 4.0
2022-02-28T11:24:34.530Z
As the main dev on `cardano-db-sync` I would recommend that you avoid building it every time you run it.

Build it once and then install it (will probably need to be manually, as root) and then it should all work (the reason it does currently work is that the service is running as `root`, and `root` does not have access to the `cabal` executable).
Erik de Castro Lopo
CC BY-SA 4.0
2022-03-01T04:02:32.873Z
Hi @ErikdeCastroLopo - thanks for your explanation! Do you mind to explain a bit more about this sentences of yours "I would recommend that you avoid building it every time you run it" - I still not really understand, which part that "build" every time I run which? *sorry, I kinda lost on that part.
wandyyd
CC BY-SA 4.0
2022-03-07T03:12:54.643Z
`cabal build ...` builds the executable. Every time you run that command the executable gets build.
Erik de Castro Lopo
CC BY-SA 4.0
2022-03-07T04:33:58.373Z
Gotcha! Thanks for your kind explanation! :)
wandyyd
<cabal><cardano-db-sync><ubuntu>
Cardano DB Sync as Service (running on background) on Ubuntu
# Cardano DB Sync as Service (running on background) on Ubuntu

(Asked by: username_0 on 2022-02-28)

Linux/Ubuntu - Cardano newbie, want to ask things around! :)
At the moment, I'm successfully installed cardano-node and can make it run as a service (pulling data automatically on the background), by following some of these instruction. I only follow some / few steps.
I tried to replicate it into cardano-db-sync version, so I can do cardano-db-sync automatically on the background. But, it gives some error / issue.
I already make, .sh and .service file.
Below is the code for, .sh file
#!/bin/bash
PGPASSFILE=/cardano/cardano-db-sync/config/pgpass-mainnet
CONFIG=/cardano/cardano-db-sync/config/mainnet-config.yaml
SOCKET_PATH=/cardano/db/node.socket
STATE_DIR=/cardano/cardano-db-sync/ledger-state/mainnet
SCHEMA_DIR=/cardano/cardano-db-sync/schema/
PGPASSFILE=${PGPASSFILE} cabal run cardano-db-sync -- --config ${CONFIG} --socket-path ${CARDANO_NODE_SOCKET_PATH} --state-dir ${STATE_DIR} --schema-dir ${SCHEMA_DIR}

And for .service file
# The Cardano DB Sync service (part of systemd)
# file: /etc/systemd/system/cardano-db-sync-run.service

[Unit]
Description     = Cardano DB Sync service
Wants           = network-online.target
After           = network-online.target

[Service]
User            = root
Type            = simple
WorkingDirectory= /cardano/cardano-db-sync
ExecStart       = /bin/bash -c '/cardano/cardano-db-sync-run.sh'
KillSignal=SIGINT
TimeoutStopSec=300
LimitNOFILE=32768
Restart=always
RestartSec=5
SyslogIdentifier=cardano-db-sync

[Install]
WantedBy        = multi-user.target

When I test to manually run /bin/bash -c '/cardano/cardano-db-sync-run.sh' it's working well and I can see some data get into database.
BUT, when I tried to run it as a service, it gives and error.
Here's what I did to make it run as a service
> sudo systemctl enable cardano-db-sync-run
> sudo systemctl start cardano-db-sync-run

And then, checking its log by
> journalctl --unit=cardano-db-sync-run --follow

And it shows an error - line 7: cabal: command not found.
I tried to see and one of my colleague advise me, to use full path of "cabal", since it isn't on /bin, so I change a bit .sh file to be
...
...

PGPASSFILE=${PGPASSFILE} /root/.ghcup/bin/cabal run cardano-db-sync -- --config ${CONFIG} --socket-path ${CARDANO_NODE_SOCKET_PATH} --state-dir ${STATE_DIR} --schema-dir ${SCHEMA_DIR}

Notice, that I change cabal into /root/.ghcup/bin/cabal. But, by doing so, it also gives an error message, as shown below
Feb 28 06:40:44 systemd[1]: Started Cardano DB Sync service.
Feb 28 06:40:44 cardano-db-sync[20548]: /cardano/cardano-db-sync
Feb 28 06:40:45 cardano-db-sync[20548]: Warning: Requested index-state 2021-07-02T00:00:00Z is newer than
Feb 28 06:40:45 cardano-db-sync[20548]: 'hackage.haskell.org'! Falling back to older state (2021-07-01T22:06:28Z).
Feb 28 06:40:45 cardano-db-sync[20548]: Resolving dependencies...
Feb 28 06:40:45 cardano-db-sync[20548]: cabal: Could not resolve dependencies:
Feb 28 06:40:45 cardano-db-sync[20548]: [__0] trying: Win32-network-0.1.0.0 (user goal)
Feb 28 06:40:45 cardano-db-sync[20548]: [__1] trying: base-4.9.1.0/installed-4.9.1.0 (dependency of Win32-network)
Feb 28 06:40:45 cardano-db-sync[20548]: [__2] next goal: base-deriving-via (user goal)
Feb 28 06:40:45 cardano-db-sync[20548]: [__2] rejecting: base-deriving-via-0.1.0.0 (conflict:
Feb 28 06:40:45 cardano-db-sync[20548]: base==4.9.1.0/installed-4.9.1.0, base-deriving-via => base>=4.14)
Feb 28 06:40:45 cardano-db-sync[20548]: [__2] fail (backjumping, conflict set: base, base-deriving-via)
Feb 28 06:40:45 cardano-db-sync[20548]: After searching the rest of the dependency tree exhaustively, these were the
Feb 28 06:40:45 cardano-db-sync[20548]: goals I've had most trouble fulfilling: base, Win32-network, base-deriving-via
Feb 28 06:40:45 systemd[1]: cardano-db-sync-run.service: Main process exited, code=exited, status=1/FAILURE
Feb 28 06:40:45 systemd[1]: cardano-db-sync-run.service: Failed with result 'exit-code'.


Again, I already try to run it manually, using /bin/bash -c '/cardano/cardano-db-sync-run.sh' and it works well, BUT, only when I make it run as a service, it shows that error.
My guess is that "cabal" isn't really recognized.
Any suggestion on why it failed?

PS --

I built cardano-node by follow this docs precisely. So, even the ghc --version and/or cabal --version is same
cardano-db-sync that I built is using Cabal (ghc stuffs), not using Nix / docker.


Thanks in advance!


*Edit #1 - after read the comments and replies from Eric de Castro Lopo and George - APEX Stake Pool, I got question
Do I need to wait this part to be done? So, I can make cardano-db-sync as a service?

PGPASSFILE=config/pgpass-mainnet cabal run cardano-db-sync -- 
--config config/mainnet-config.yaml 
--socket-path ../cardano-node/state-node-mainnet/node.socket 
--state-dir ledger-state/mainnet 
--schema-dir schema/

*It's written on the docs


*Edit #2 - after checking more
Question from *Edit #1 - can be ignored, but for someone who looking for the answer, the answer is didn't need to. Detail can be seen on comment / replies by others.
Anyway, finally I can make it work as service. And as per guidance from Eric de Castro Lopo and George - APEX Stake Pool, seems I mistakenly put the syntax/command on .sh file as mentioned. Thanks!
In-case someone need, the workaround I did is to make a symlink of the executable file of cardano-db-sync

Do find . -name cardano-db-sync -executable -type f to find its location
Then, I did symlink (register cardano-db-sync) into /usr/local/bin (or wherever suitable for you), using sudo ln -s  ... (or other similar syntax / command).
And finally, on .sh file, I change this part ... cabal run cardano-db-sync -- ... into ... cardano-db-sync -- ...


Anyway, I will mark George - APEX Stake Pool's answer as it's the closest hint for me.
And, sorry Eric de Castro Lopo that I couldn't upvote your comment, as it's helpful too for me! Need +15 rep to upvote your comment :(
Thanks for your time! :)

**Tags:** cabal, cardano-db-sync, ubuntu
> **Comments:**
> **username_1**: As the main dev on `cardano-db-sync` I would recommend that you avoid building it every time you run it.  Build it once and then install it (will probably need to be manually, as root) and then it should all work (the reason it does currently work is that the service is running as `root`, and `root` does not have access to the `cabal` executable).  
> **username_0**: Hi @ErikdeCastroLopo - thanks for your explanation! Do you mind to explain a bit more about this sentences of yours "I would recommend that you avoid building it every time you run it" - I still not really understand, which part that "build" every time I run which? *sorry, I kinda lost on that part.  
> **username_1**: `cabal build ...` builds the executable. Every time you run that command the executable gets build.  
> **username_0**: Gotcha! Thanks for your kind explanation! :)  

---

## 1 answers

### Answer by username_2 on 2022-02-28. Score: 6 | Accepted answer

Why are you running cardano-db-sync with cabal? You should just run cardano-db-sync or cardano-db-sync-extended.
And another question, more important: why are you running the service as root? This is always a security issue. You should never run a service as root (unless really needed, which is not the case here) or as an user with sudo rights. And cardano-db-sync should be in /usr/local/bin, for example.
I'm posting here my service file and my shell script executed by the service:
# cat /etc/systemd/system/cardano-db-sync.service 
# The Cardano db-sync service (part of systemd)
# file: /etc/systemd/system/cardano-db-sync.service 

[Unit]
Description      = Cardano db-sync service
Wants            = network-online.target
After            = network-online.target 

[Service]
User             = cardano
Type             = simple
WorkingDirectory = /home/cardano/cardano-node
ExecStart        = /bin/bash -c '/home/cardano/cardano-node/bin/db-sync.sh'
KillSignal       = SIGINT
TimeoutStopSec   = 120
LimitNOFILE      = 32768
Restart          = always
RestartSec       = 5

[Install]
WantedBy         = multi-user.target

# cat /home/cardano/cardano-node/bin/db-sync.sh
PGPASSFILE=/home/cardano/cardano-node/config/pgpass-mainnet cardano-db-sync-extended \
    +RTS -N -RTS \
    --config /home/cardano/cardano-node/config/mainnet-db-sync-config.json \
    --socket-path /home/cardano/cardano-node/db/node.socket \
    --state-dir /home/cardano/cardano-node/ledger-state/mainnet \
    --schema-dir /home/cardano/cardano-node/schema/

> **Comments:**
> **username_0**: Hi! Thanks for your explanation and sample codes. It kinda gives me more explanation. About your question "Why are you running `cardano-db-sync` with cabal? You should just run `cardano-db-sync` or `cardano-db-sync-extended` - it seems I kinda misunderstood the [documentation](https://github.com/input-output-hk/cardano-db-sync/blob/master/doc/building-running.md). And, because its last step, I kinda confuse on where can I found the "cardano-db-sync-exe", so, I follow the previous step where it says "... cabal run cardano-db-sync .... "  
> **username_0**: --continue-- About your concern, why I run it as root, it's because the server is a test server, and I still learn things around since I super newbie! Anyway, thanks a lot for that! really means a lot for me to learn! || And "And cardano-db-sync should be in /usr/local/bin" - I don't see that on that folder. I just follow their [docs](https://github.com/input-output-hk/cardano-db-sync/blob/master/doc/building-running.md#run-two-chains-with-a-single-postgresql-instance).  
> **username_2**: You are welcome. I am keeping all cardano-related binaries in /usr/local/bin, it's probably the best location for them.  
> **username_0**: ohh! I see! I thought it supposed to be there after installation. Thanks for your tips!  

---

