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 privilegespostgres=# \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
/home/saurabh/projects/cineapi/cineapi.o
, but under which Linux user will it be invoked?User
andGroup
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 addedUser=saurabh
andGroup=saurabh
tocineapi.service
, I'm getting error:saurabh@saurabh FATAL: database "saurabh" does not exist
..env
file you prepared? (I'm not very familiar withpsql
so I might be mistaken to search in that particular direction)PGUSER
,PGPASSWORD
,PGHOST
, andsource ~/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
..bashrc
.