How do I create a postgresql container only to be accessed by ssh port 22

I need to run a container with postgresql, and set it up to only accepts requests by ssh.

What do you mean by “requests by ssh”? Do you want to allow the access to the database only to people who have SSH access to the server on which the container is running? If you do, you can forward a local port on you server to the container (docker run -p ...) and use SSH tunnel like this:

ssh -L -N yourserver@yourhostname

Leave that terminal open and connect to the database using as host on your client machine. I have never used SSH tunnel for Postgres, but I always do it for MySQL. Some database manager GUI also have built-in support for SSH tunnel so you don’t need to do in in the terminal.

Note: You could use where I used I just wanted to show that you don’t need to use the same.