Recover data from Postgresql database after failed system update

0

First, I want to make clear that this question was already asked in askubuntu, but I proceeded to delete it because I got almost no feedback. If this violates SU protocol, please let me know.


Welcome to my mess...

The situation is:

  • Ubuntu Software was making some "important" updates (and probably a lot of them, since was easily more than 9 months since my last update).

  • The update seemed to stop working (update progress bar frozen), with no explanation of what was happening. There is a notification of some error that didn't give me much information or anything to do about it, save for clicking on the "Report" button.

  • After around 8 hours of using the PC and hoping that the issue would resolve itself, I note that the process bar is still frozen. I cross my fingers and shut down the computer.

  • Now laptop doesn't boot on Ubuntu (Still has Windows, didn't try to boot that, but I guess it still works).

  • Big and important Postgresql database is stored in laptop, on /var/lib/postgresql/9.5/main (yeah, I was really tired the day before when I shut down the laptop, didn't anticipate this... really stupid thing to do; don't do this at home kids).

  • Made a bootable usb stick with Ubuntu 16.04 live, with the intention of accessing my files and make a backup, before re-installing Ubuntu.

  • Can't backup mentioned database (meaning, can copy/paste the folder with Nautilus or via terminal+sudo), it says the contents of /var/lib/postgresql/9.5/main are unreadable (I don't have permissions). With my home directory there are fewer problems and I manage to make a backup (with a couple of unimportant exceptions).

What can I do about this? Is it possible to copy the files from the database to make a backup? Furthermore, how can I access this database afterwards?

Thanks for your time...


Update: I succesfully backed up my files using sudo /bin/bash and then cp, as davidgo suggested (saved them into an external hard drive).

Then I installed ubuntu from scratch in my PC, installed posgresql-9.5 (adding the Postgres PGDG repo mentioned by davidgo), and copied the files to the same original location (using cp -rnP /pathto/backupfolder /var/lib/postgresql/9.5/main) and changed the ownership to "postgres".

Now my problem is that I cannot start the postgresql client (psql). This is the command line output:

user@machine:~$ psql
psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Looking up the documentation, I found that I could use pg_ctl to start the server, but that in Ubuntu, is expected that I use pg_ctlcluster instead. Ok, so I try this:

user@machine:~$ su - postgres
Password: 
postgres@machine:~$ pg_ctlcluster 9.5 main status
pg_ctl: server is running (PID: 1034)
/usr/lib/postgresql/9.5/bin/postgres "-D" "/var/lib/postgresql/9.5/main" "-c" "config_file=/etc/postgresql/9.5/main/postgresql.conf"

I am a rather clumsy PostgreSQL user, I can tell, so this is probably something real simple that I cannot see right now. Again, any help would be more than wecome!

Juan

Posted 2017-05-05T19:38:38.740

Reputation: 101

2I assume you don't have no backups of this important database? – Ramhound – 2017-05-05T19:48:15.597

No, I don't have a backup. I could rebuild it with enough time (a couple of weeks I think), but that's what I'm trying to avoid. – Juan – 2017-05-05T19:50:48.280

If you have no backups of the database nor any images of the system itself, you are sort of up the creek without a paddle. If you already installed Ubuntu over your existing installation your data isn't going to be there. If it says you don't have permissions you should give yourself permissions. – Ramhound – 2017-05-05T19:54:23.593

At this point I still have my original Ubuntu installation (broken, but all the files are there). Regarding the permissions, I do not know how can I do that, since I can't boot from said original installation and postgresql client or the command line (Ubuntu recovery mode doesn't work either). – Juan – 2017-05-05T19:57:24.703

1This is the time you thank God that you have a recent backup. – SDsolar – 2017-05-06T02:13:21.360

Answers

2

In order to get this working, the steps would be:

  1. Install a new copy of UBUNTU - onto a new drive, so as not to overwrite the postgres files. Install Postgres 9.5 on the new instance (You may or may not need to add the Postgres PGDG repo to do this)

  2. Copy the old Postgres files onto the new system to the same location (typically /var/lib/pgsql/9.5) . As you are having difficulties here, you should try doing it AS ROOT from the command line. (I assume SELinux is not operative or interfering, if it is, disable it). [You can either put the disk in as a second drive, or use a USB enclosure] To get root access, type "sudo /bin/bash"

  3. Change the ownership permissions of the postgresql files to the postgres user.

  4. Start Postgres!

davidgo

Posted 2017-05-05T19:38:38.740

Reputation: 49 152

A little late, but thanks a lot! The trick with sudo /bin/bash was key to make a backup, but I have new problems now, described in the update part of my post. If you can check it and give me any suggestion I would be very grateful. – Juan – 2017-05-19T17:43:43.260

A couple of things - to run Postgres you gave 2 parts - launching the server and connecting to it with clients. psql is a client, and is complaining that it can't connect to the server. I'm guessing pg_ctlcluster is doing this for you, but I have never used it - I prefer /etc/inot.d/postgres start (or similar) which automatically backgrounds it. The thing is once Postgres is running, launch a running, launch psql in a second window. If you have tried this and it is not working, what happens? – davidgo – 2017-05-19T19:14:06.947

0

in my case it was the shared_buffers was too big that cased the failed state, I decrease it to 128MB and restarted then worked fine , I made a copy of the data directory after an OS crash to a new OS ..

Mawardy

Posted 2017-05-05T19:38:38.740

Reputation: 101

Can you describe exactly how to do this?   What commands, etc.?  Please do not respond in comments; [edit] your answer to make it clearer and more complete. – Scott – 2019-01-31T00:49:54.847