1

I have an application running on Ubuntu server that's using PostgreSQL.

I can run the application's executable (as a non-root user) from the project's directory.

I have created a service for this application (/etc/systemd/system/cineapi.service), so that it starts automatically on reboot. The contents of this file are:

[Unit]
Description=cineapi

[Service]
Type=simple
Restart=on-failure
RestartSec=5s
WorkingDirectory=/home/saurabh/projects/cineapi
ExecStart=/home/saurabh/projects/cineapi/cineapi.o

[Install]
WantedBy=multi-user.target

When a reload the system daemon and start the service like:

sudo systemctl daemon-reload
sudo service cineapi start

I'm getting an error:

pq: password authentication failed for user "root"

I dont understand why this is happening. I'm not using the "root" user anywhere in the application. It's .env file looks like

postgres://goapi:somepassword@MY_IPV4_ADDR:DB_PORT/cineapi

and is sourced at startup. I also have the following changes in:

  • /etc/postgresql/16/main/postgresql.conf

    password_encryption = scram-sha-256
    
  • /etc/postgresql/16/main/pg_hba.conf

    host    cineapi         goapi           MY_IPV4_ADDR            scram-sha-256
    
  • DB user goapi has superuser privileges

    postgres=# \du
                               List of roles
    Role name |                         Attributes                         
    ----------+------------------------------------------------------------
    goapi     | Superuser, Create DB
    postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS
    

Logs

Postgres logs when executing the binary:

2024-02-24 18:42:31.053 PST [16433] LOG:  checkpoint starting: time
2024-02-24 18:42:36.817 PST [16433] LOG:  checkpoint complete: wrote 60 buffers (0.4%); 0 WAL file(s) added, 0 removed, 0 recycled; write=5.737 s, sync=0.014 s, total=5.765 s; sync files=54, longest=0.003 s, average=0.001 s; distance=201 kB, estimate=201 kB; lsn=0/1A477E0, redo lsn=0/1A477A8
2024-02-24 18:43:39.173 PST [16573] goapi@cineapi LOG:  could not receive data from client: Connection reset by peer             #### ---------------- I stopped the executable ----------------- ####

Postgres logs when starting the application's system service

2024-02-24 18:37:31.024 PST [16432] LOG:  database system is ready to accept connections
2024-02-24 18:38:28.926 PST [16473] root@root FATAL:  role "root" does not exist
7
  • 1
    You've configured systemd to invoke the executable /home/saurabh/projects/cineapi/cineapi.o, but under which Linux user will it be invoked?
    – Sotto Voce
    Commented Feb 25 at 19:15
  • Looks like when User and Group are not mentioned in the service file, root gets selected by default. Since I'm able to run the executable directly with my user and group - saurabh:saurabh. So I've added User=saurabh and Group=saurabh to cineapi.service, I'm getting error: saurabh@saurabh FATAL: database "saurabh" does not exist.
    – Saurabh
    Commented Feb 25 at 21:02
  • Could that new error be consistent with the command not reading the .env file you prepared? (I'm not very familiar with psql so I might be mistaken to search in that particular direction)
    – Sotto Voce
    Commented Feb 26 at 0:55
  • I have PGUSER, PGPASSWORD, PGHOST, and source ~/projects/cineapi/.env at the end of my .bashrc. The former values correspond to what's set in the .env file. However, I'm not sure whether .bashrc sourcing this file is being applied to /etc/systemd/system/cineapi.service.
    – Saurabh
    Commented Feb 26 at 1:20
  • I don't think its consistent because of my config in .bashrc.
    – Saurabh
    Commented Feb 26 at 1:27

0

You must log in to answer this question.

Browse other questions tagged .