deleting files in /var/lib/postgresql/12/main while attempting to replication in postresql

Viewed 23

I am new to postgres and was following this tutorial for setting Up Physical Streaming Replication with PostgreSQL

In step 3 while running the following command: sudo -u postgres rm -r /var/lib/postgresql/12/main/*

I was getting the following error

rm: cannot remove '/var/lib/postgresql/12/main/*': No such file or directory while the /var/lib/postgresql/12/main/ clearly had many files if explored manually.

In desperation, I deleted all the files inside /var/lib/postgresql/12/main/ manually and now any of the further steps are not working.

I have even tried to uninstall and install postgresql-12 using

sudo apt-get --purge remove postgresql and sudo apt -y install postgresql-12 postgresql-client-1 respectively I have even tried doing the whole process again from start and while running the following command:

sudo -u postgres psql

sudo pg_ctlcluster 12 main start

I got this error:

Job for postgresql@12-main.service failed because the service did not take the steps required by its unit configuration.
See "systemctl status postgresql@12-main.service" and "journalctl -xe" for details.

while resolving the above issue using :

sudo chown postgres.postgres /var/lib/postgresql/12/main/global/pg_internal.init

I got this error...

chown: cannot access '/var/lib/postgresql/12/main/global/pg_internal.init': No such file or directory

I think this is happening because of the manual deletion of all the files and folder in /var/lib/postgresql/12/main/

Any help is much appreciated Thanks

1 Answers

The glob * is evaluated by your regular user before the sudo is invoked, but your regular user can't see into that directory. So what gets sent to the postgres user is an order to remove the file with the literal name of '/var/lib/postgresql/12/main/*', which doesn't exist. You would need to have your shell that evaluated the glob be postgres, so it can see what it is doing before invoking rm. Something like:

 sudo -u postgres bash -c 'rm -r /var/lib/postgresql/12/main/*'

For the rest of it, you didn't give enough details to know what is going on, like what was in the logs, or what were the directory listings at the time your command failed.

Related