First of all, I sincerely regret for you; I was in your situation only a few months ago.
Whether you think you're leaving or staying, FIND A NEW WORK NOW! This is the safest way for CIA and a new job offer is your capital to fight the bully without restraint and without devastating consequences if you lose the fight.
In the meantime, do not give him the opportunity to attack you emotionally. If you never do your acts in the presence of certain people (eg the CEO), take note of this so that when those people are absent and your manager is here, you know going to landfill or join a chat with the water somewhere else. You want to preserve your physical and mental health as you fight this long and ugly battle.
If it comes in your cubicle because you just saw the code you assembled in less than 5 minutes to do an ad hoc test that you shared with your team on Slack, and declares with a voice strong enough to hear the whole office : "you wrote this error, do you even have the code?" You use it through the bad but pragmatic code and you talk to it with an equally strong voice "… So in this specific case, we get to 1 + 1 = 2. 1 + 1 is not equal to 2?"He would adjust his tone, smile and try to change the subject or concentrate on other irrelevant aspects of the code, because he simply made himself ridiculous.
If your manager is also technical and writes programs as part of his job, check his code. You might find vulnerabilities to SQL injections everywhere, adjustable amateur crap with just two lines of extra code. Hack it and try it (remove a table you created specifically to demonstrate this security hole). Wait for the right time to make it public (or private).
If your manager has ever referred to his subordinates as "brats" in IM, take a screenshot of that and save copies of them. You could even secretly register your manager by verbally abusing you if you want to take the risk. Yes, it is illegal to record a conversation without the knowledge and consent of the participants, but you need all the evidence you can get your hands on to expose that it is a real "manager" and how its toxicity will ruin the company . However, consider doing so only if you are sure that the CEO is neither stupid, neither soft nor evil, and even if the manager does not hold the company hostage, since, if possible, together with the allies, you will speak directly with the CEO as soon as you receive a decent / excellent job offer letter and present the manager as a manager / risk and an employee totally replaceable to the company and help the CEO to come to the conclusion to dismiss this bully, or resign the resignation on the spot and wish the CEO good luck if this does not work.
Your manager will not stop undermining you. It will give you tasks with tight deadlines or impossible, or tasks that have little or no visibility if you do a good job but high visibility (negative) if you do a bad job, or both. For example, keeping the RDMS databases with very high read and write volumes when you are not even an aspiring DBA debutant.
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 an old staff who would soon be joined by a bully executive, so I did not follow my suggestion to bring this question to the CEO. Therefore, I do not know if what I have said is valid or even realistic, since I am not very experienced in office politics. But luck was on my side and now I'm working on the best job I've had in the last 4 or 3 years even though I got a verbal job offer only when I gave my communication because I could not take it anymore. I would like things to work for you too. You do not deserve to be treated like shit; he does.