First, I'm sincerely sorry for you; I've been in your situation just a few months ago.
Whether you plan to leave or stay, FIND A NEW JOB NOW! This is the surest way to CYA and a new job offer is your capital to fight the bully without restraint and without devastating consequences should you lose the fight.
Meanwhile, don't give him the opportunity to emotionally attack you. If he never does his acts in the presence of certain people (e.g. the CEO), make a note of that so that when those people are away and your manager is here, you know to go take a dump or join water cooler chat somewhere else. You want to preserve your physical and mental health as you fight this long, ugly battle.
If he comes to your cubicle because he just saw the code you've assembled under 5 minutes to do an ad hoc one-time test that you shared with your team on Slack, and declares in a voice loud enough for the whole office to hear: "you wrote this wrong, do you even code?" You work him through the lousy but pragmatic code and speak to him in an equally loud voice "... So in this specific case, we arrive at 1 + 1 = 2. Is 1 + 1 not equal to 2?" He would soften his tone, smile at you and try to change the topic or focus on other irrelevant aspects of the code because he just made a fool of himself.
If your manager is also technical and writes programs as part of his job, review his code. You might find vulnerabilities to SQL injections everywhere, amateurish crap fixable with only an extra two lines of code. Hack it and try it (delete a table you created specifically to demo this security hole). Wait for the right opportunity to make a public (or private) display of it.
If your manager ever referred to his subordinates as "brats" in IM, take a screenshot of that and save copies of it. You could also secretly record your manager verbally abusing you if you want to take the risk. Yes, it is illegal to record a conversation without the participants' knowledge and consent, but you need all the evidence you can get your hands on to expose what an a-hole your "manager" really is and how his toxicity will ruin the company. However, only consider doing this if you are sure the CEO is neither stupid, soft nor evil, and also not if that manager is keeping the company hostage, as, together with allies if possible, you're going to talk directly to the CEO as soon as you receive a decent/great job offer letter and present the manager as a liability/risk and totally replaceable employee to the company and help the CEO arrive at the conclusion to fire this bully, or resign on the spot and wish the CEO good luck if this doesn't work out.
Your manager will not stop undermining you. He will give you tasks with tight or impossible deadlines, or tasks that have little to no visibility if you do a great job but high (negative) visibility if you do a bad job, or both. For example, maintaining RDMS databases with very high read and write volumes when you're not even an aspiring DBA rookie.
I was able to judge the CEO at my last job was stupid in my first meeting with him, and his company was held hostage by a senior staff who would soon be joined by a manager bully, so I didn't follow through with my suggestion of bringing this matter to the CEO. Therefore, I don't know if what I've said is sound or even realistic, as I'm not well-versed in office politics. But luck was on my side and I'm now working the best job I've had out of my last 4 in 3 years despite only obtaining a verbal job offer when I gave my notice because I couldn't take it anymore. I wish things work out for the better for you, too. You don't deserve to be treated like shit; he does.