I just have done

pg_basebackup -h primary_host -p primary_port -D PGDATA -U replication user

and I jsut wanted to restart the db, so I did

sudo su - postgres

sudo service postgres restart

and the ec2 says to me

Respect the privacy of others, think before you type, ...

and it asks me password for postgres. But since I have created this ec2 instance, I have never set any password for postgres. So there is no password for this actually, but it keeps asking me...

I guess there might be something related to pg_basebackup, but I have no idea, because the primary db also has no specific password..

What should I do here, thanks

  • I dont think so, it says '[sudo] password for postgres:' Mar 6, 2023 at 8:23
  • Services are managed by root user, so use sudo su - and then restart Mar 6, 2023 at 8:25
  • Then true, you've run the second command from the shell given to you by the first one. This is wrong, don't do it. Mar 6, 2023 at 8:25

1 Answer 1


When you run

sudo su - postgres

It invokes a new shell, which is owned by postgres user.

Then, from that shell you run another sudo:

sudo service postgres restart

And it, unsurprisingly, asks password for the current system user, which is postgres, because you ran it from the shell that belongs to postgress. Again, this is not because you set or did not set password for postgres, and this is not about database user postgres, but the OS account, whose shell was invoked with the first command.

Just skip the first command, or exit that shell when you've finished doing things as postgres system account. You should manage services with sudo from your normal admin user, not from postgres user.

  • thanks for your reply. But may I ask one more question here? so, inside the su - postgres bash, when I type 'psql --version', then it prints the version. but when I type 'psql -U root' or just 'psql', then it says 'psql: could not connect to server: No such file or directory'... when I type 'sudo pg_ctl -D /var/lib/pgsql/data start', it says 'pg_ctl: could not open PID file: permission denied' anyway I need to connect to the pgsql prompt... Mar 6, 2023 at 8:37
  • Run psql -U postgres from that postgres-user shell. Mar 6, 2023 at 8:38
  • Thanks it was the problem of the ownership of the directory Mar 6, 2023 at 8:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .