postgres

Is it running: service postgresql status

cd /etc/postgresql/14/main
sudo cat pg_hba.conf

Make it end like this:

# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database superuser can access the database using some other method.
# Noninteractive access to all databases is required during automatic
# maintenance (custom daily cronjobs, replication, and similar tasks).
#
# Database administrative login by Unix domain socket
local   all             postgres                                trust

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

Do this to restart postgres:

sudo service postgresql stop
 * Stopping PostgreSQL 14 database server [ OK ]

sudo service postgresql start
 * Starting PostgreSQL 14 database server [ OK ]

And you are back in business:

$ psql -U postgres
psql (14.9 (Ubuntu 14.9-0ubuntu0.22.04.1))
Type "help" for help.

postgres=#

Now set the new password:

ALTER USER postgres WITH PASSWORD <password>;