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.

Writing the same regular expression logic in multiple JVM languages

I tried writing three regular expressions in a the most common JVM languages.

  1. Find first match
  2. Find all matches
  3. Replace first match

My experience in these languages range from use it many times a week (Groovy) to this is the first thing I’ve written in it (Clojure).

I’m going to be using these in a presentation. So if you see anything in here that is a bad idiom in the language, do let me know!

Kotlin

The WordPress syntax highlighter doesn’t have Kotlin as a choice

val text = "Mary had a little lamb"
val regex = Regex("\\b\\w{3,4} ")
print(regex.find(text)?.value)
-----------------------------------------
val text = "Mary had a little lamb"
val regex = "\\b\\w{3,4} ".toRegex()
regex.findAll(text)
  .map { it.groupValues[0] }
  .forEach { print(it) }
-----------------------------------------
val text = "Mary had a little lamb."
val wordBoundary = "\\b"
val threeOrFourChars = "\\w{3,4}"
val space = " "
val regex = Regex(wordBoundary +
  threeOrFourChars + space)
     
println(regex.replaceFirst(text, "_"))

Scala

Thanks to dhinojosa for the code review and feedback that smart quotes don’t require backslashes inside!

val text = "Mary had a little lamb"
val regex = """\b\w{3,4} """.r
val optional = regex findFirstIn text
      
println(optional.getOrElse("No Match"))
-----------------------------------------
val text = "Mary had a little lamb."
val regex = """\b\w{3,4} """.r
val it = regex findAllIn text
      
it foreach print
-----------------------------------------
val text = "Mary had a little lamb."
val wordBoundary = """\b"""
val threeOrFourChars = """\w{3,4}"""
val space = " "
val regex = new Regex(wordBoundary + threeOrFourChars + space)
     
println(regex replaceFirstIn(text, "_"))

Closure

(println(
  re-find #”\b\w{3,4} ", 
          "Mary had a little lamb"))
-----------------------------------------
(println(
  re-seq #”\b\w{3,4} ", 
          "Mary had a little lamb"))
-----------------------------------------
(ns clojure.examples.example
   (:gen-class))
(defn Replacer []
   (def text "Mary had a little lamb.")
   (def wordBoundary "\\b")
   (def threeOrFourChars "\\w{3,4}")
   (def space " ")
   (def regex (str wordBoundary 
        threeOrFourChars space))
   (def pat (re-pattern regex))
   (println(clojure.string/replace-first 
       text pat "_")))
(Replacer)

Groovy

def text = 'Mary had a little lamb'
def regex = /\b\w{3,4} /

def matcher = text =~ regex
print matcher[0]
-----------------------------------------
def text = 'Mary had a little lamb'
def regex = /\b\w{3,4} /

def matcher = text =~ regex
print matcher.findAll().join(' ')
-----------------------------------------
def text = 'Mary had a little lamb'
def regex = /\b\w{3,4} /

def matcher = text =~ regex
print matcher.findAll().join(' ')