Fixing postgres

When upgrading my Mac, Postgres stopped working. This page documents my journey. First, the migration process gave me a long password for the postgres user. It also didn’t set up Postgres to start up automatically. netstat confirmed nothing running on 5432.

Posgres is started on the old machine using a launch daemon. I can see it at /Library/LaunchDaemons/postgresql-15.plist. Launch daemons require elevated access to run which seems related to the user recreation.

The plist file is a text file so I read it. It says that it runs at load with userName postgres. I ran dscl . list /Users and confirmed that user still exists.

I then came across this post which says I shouldn’t expect the database to start up from a time machine backup. Given that this is just a test database and there’s nothing in there I care about, I tried reinstalling Postgres. Which didn’t change anything.

I then checked /Library/PostgreSQL/15/data/log/<latest> and got:

2023-05-21 07:51:02.282 EDT [372] LOG:  listening on IPv6 address "::", port 5432
2023-05-21 07:51:02.282 EDT [372] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2023-05-21 07:51:02.282 EDT [372] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2023-05-21 07:51:02.300 EDT [402] LOG:  database system was interrupted; last known up at 2023-04-25 20:00:54 EDT
2023-05-21 07:51:02.667 EDT [402] LOG:  invalid record length at 0/331FC48: wanted 24, got 0
2023-05-21 07:51:02.667 EDT [402] LOG:  invalid primary checkpoint record
2023-05-21 07:51:02.667 EDT [402] PANIC:  could not locate a valid checkpoint record
2023-05-21 07:51:02.668 EDT [372] LOG:  startup process (PID 402) was terminated by signal 6: Abort trap: 6
2023-05-21 07:51:02.668 EDT [372] LOG:  aborting startup due to startup process failure
2023-05-21 07:51:02.668 EDT [372] LOG:  database system is shut down

That was helpful. I reset the log.

JeanneBrskysMBP:bin postgres$ ./pg_resetwal ../data
The database server was not shut down cleanly.
Resetting the write-ahead log might cause data to be lost.
If you want to proceed anyway, use -f to force reset.
JeanneBrskysMBP:bin postgres$ ./pg_resetwal -f  ../data
Write-ahead log reset

After restarting, I saw postgres up again. Yay.

nyjeanne@JeanneBrskysMBP ~ % netstat | grep 5432
222187ba86974afb stream      0      0 222187b5bb6cdf9b                0                0                0 /tmp/.s.PGSQL.5432

And the startup log is happy now.

2023-05-21 08:09:08.930 EDT [374] LOG:  database system is ready to accept connections
2023-05-21 08:14:08.987 EDT [403] LOG:  checkpoint starting: time
2023-05-21 08:14:08.992 EDT [403] LOG:  checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.002 s, sync=0.001 s, total=0.005 s; sync files=2, longest=0.001 s, average=0.001 s; distance=0 kB, estimate=0 kB

The other problem is that I had removed the database from pgadmin. Luckily this doesn’t delete the database. I registered it again with the same info as the settings on the old mac and all was well. My data is still there too. Nice!

setting up a mac in 2023 from my old mac

My previous Mac only lasted four years. It’s successor made it much longer. At the 8 year mark, the battery was failing. I could have had it replaced for $250, but didn’t thought it was better to get a new machine rather than buy a battery and have the machine need replacement shortly after.

My choice

I had a 15 inch laptop last time, but went with the 13.6 inch. The retina display makes it feel similar, I was pleasantly surprised that the Air was so powerful, especially when adding to it. The specs:

  • Apple M2 chip with 8‑core CPU, 10‑core GPU, 16‑core Neural Engine
  • 24GB unified memory (I had 16 last time)
  • 1TB SSD storage (I had half that before and used a good amount)

Buying the laptop

I went to the store to see the size and feel. I also needed to ask about the difference in colors and compare to my old machine. I went with Space Gray which is a color darker than what I had before.

In the store, they suggested I buy it online so I would know when it comes. (It wound up coming earlier.) I was still able to have it delivered to the store so that was fine.

Other hardware needed

I needed a USB-C to USB-A hub so I could plug in my existing devices. My wired keyboard and wired trackpad still work. I got a hub that also had HDMI so I could plug in my external monitor.

I also got a single USB-C to USB-A adapter so I can continue to use my clicker for presentations.

I still have to set up a security lock. I ordered the Kensington one. There weren’t a lot of choices and it doesn’t let me reuse my existing lock. The new one doesn’t allow closing the lid while the laptop is docked which will take some getting used to.

Setting up the laptop

Time Machine took care of most of the transfer and chip architecture differences. It ran most of the night to transfer all the data. It asks whether you want to start a separate backup or claim the history. I chose the later.

What I had to do:

  1. Cover camera with sticker.
  2. Agree to assorted licenses
  3. Dropbox had me reconfirm
  4. Reinstall key for SnagIt
  5. A lot of setting changes (see next section)
  6. I’ve had the laptop long enough to label it with my phone number and add stickers.
  7. That’s it. Everything else was remembered.

Settings need to reapply

Eight years ago I didn’t need to redo so many settings.

  1. Bring up brightness level to max. Hard to read at default resolution when not bright
  2. Chrome – changed startup settings to remember tabs
  3. Postgres – had to set a new password
  4. A lot of privacy/security settings. Many apps reprompted to get their permissions back

Updating apps

  1. IntelliJ offered to update for the new chip.
  2. Eclipse needed a reinstall with AArch 64 (my Eclipse was from 2021 though – I use a mix of IntelliJ and Eclipse). The installer noted I have a lot of Java versions installed and complained about the signatures for some. I chose cancel and it proceeded. I also had to delete the .m2 cache because Eclipse prompted me it wasn’t compatible.
  3. Zoom offered to update for the new chip
  4. I had to re-install BeFocusedPro to get it to be in the menu bar. It remembered I paid already at least.
  5. I installed 1Password 8. I don’t think this was necessary, but I couldn’t figure out the settings to have it open web pages in 1Password 7. And I would have upgraded soon anyway. I also needed to set up Universal Autofill on Mac so I could use command + \. (It used to automatically prompt me to use 1Password. I like the keyboard shortcut though)

Postgres

This was a lot so gets its own post.

Specialist appointment

Apple encourages a free online appointment with a “specialist” to go over new features. I did that and learned a few things.

Touch Id

The built in keyboard has physical keys. (not the touchbar of the past; glad I missed that model.) On the top right (next to F12) is a touch key for touch id. (My old machine had a power button there) So far, I’ve used it for 1Password, unlocking after initial bootup for the day and Apple settings. I also set up a second finger to work with touch id.

Twitter

I can’t logon to Twitter using the official app. It still remembers me as @jeanneboyarsky and asks for a password to verify my identity. Then gives an error. I tried uninstalling and reinstalling. My browser session still works so I’m afraid to change my password to see if that fixes. It’s not a big deal to use the web app. I mainly browse on my phone/tablet, so it is only for posting on the computer anyway.

recovering postgres from a time machine backup

Last night, I needed to do a full Time Machine restore on my Mac. I messed up installing things and made the problem far worse in my attempt to “fix” it. Then I tried to rollback a fraction of the disk. The laptop predicted 11 hours to rollback the files. It was easier to rollback to the state a few hours earlier. The only time I did a full Time Machine restore was when I got a new computer. That was going forward in calendar time though. This is the first time I went backwards.

For the most part, it was easy. The Mac prompted me on whether it was ok to erase the disk and restore. That took about two hours. My computer was pretty much set up on restore. It remembered lots down to my internet connection and github key. I got re-prompted from my Dropbox credentials (and for two factor.) No big deal.

Then there was Postgres. Every few seconds, I got the message “Do you want the application “postgres” to accept incoming network connections?” The message only appeared for about a second. Even if I was fast enough to click on it, it was back a few seconds later.

I tried signing the app based on some internet posts. No luck.

Temporary Relief

I got temporarily relief while I looked into the problem by blocking all connections:

  • System Preferences
  • Security & Preferences
  • Click the lock to make changes
  • My password
  • Click unlock
  • Firewall Options
  • Click “Block all incoming connections”
  • Ok

This is not helpful in the long run, but it let me look at things without going crazy.

The problem

I checked the database logs in /Library/PostgreSQL/8.4/datapg_log and found a file every few seconds with:

2016-04-25 20:39:15 EDT LOG:  database system was interrupted; last known up at 2016-04-24 09:18:51 EDT
2016-04-25 20:39:15 EDT LOG:  record with zero length at 0/5C42472C
2016-04-25 20:39:15 EDT LOG:  invalid primary checkpoint record
2016-04-25 20:39:15 EDT LOG:  record with zero length at 0/5C4246E8
2016-04-25 20:39:15 EDT LOG:  invalid secondary checkpoint record
2016-04-25 20:39:15 EDT PANIC:  could not locate a valid checkpoint record
2016-04-25 20:39:15 EDT LOG:  startup process (PID 2326) was terminated by signal 6: Abort trap
2016-04-25 20:39:15 EDT LOG:  aborting startup due to startup process failure

Ah ha! That makes perfect sense. I jumped around in time which would confuse a transaction log. Now,t hat’s something I know how to fix. I did a forced reset of the transaction log:

  1. sudo su postgres
  2. cd /Library/PostgreSQL/8.4/bin
  3. ./pg_resetxlog -f /Library/PostgreSQL/8.4/data

And all better! I turned the firewall connections back to the way they were before I started and good as new.