# The $postgres_usr_pw and the other Bash variables MUST be defined# for reference the manual way of doing things automated with expect bellow#echo "copy-paste: $postgres_usr_pw"#sudo -u postgres psql -c "\password"# The OS password could / should be differentsudo -u root echo "postgres:$postgres_usr_pw" | sudo chpasswd
expect <<- EOF_EXPECTset timeout -1spawn sudo -u postgres psql -c "\\\password"expect "Enter new password: "send -- "$postgres_usr_pw\r"expect "Enter it again: "send -- "$postgres_usr_pw\r"expect eofEOF_EXPECT
cd /tmp/# At this point the 'postgres' executable uses the new passwordsudo -u postgres PGPASSWORD=$postgres_usr_pw psql \--port $postgres_db_port --host $postgres_db_host -c "DO \$\$DECLARE r record;BEGINIF NOT EXISTS (SELECTFROM pg_catalog.pg_rolesWHERE rolname = '"$postgres_db_useradmin"') THENCREATE ROLE "$postgres_db_useradmin" WITH SUPERUSER CREATEROLECREATEDB REPLICATION BYPASSRLSPASSWORD '"$postgres_db_useradmin_pw"' LOGIN ;END IF;END\$\$;ALTER ROLE "$postgres_db_useradmin" WITH SUPERUSER CREATEROLECREATEDB REPLICATION BYPASSRLSPASSWORD '"$postgres_db_useradmin_pw"' LOGIN ;"
可以通过检查系统d命令行找到数据目录的文件夹,使用systemctl status postgresql@VERSION-DB_CLUSTER很容易获得。将VERSION替换为您的psql版本,并DB_CLUSTER数据库集群的名称。如果它是自动创建的,则可能是main,因此,例如,postgresql@13-main。或者,我的Bash shell在输入postgresql@后提供了自动完成,因此您可以尝试或在所有服务列表中查找PostgreSQL服务(systemctl -a)。一旦您有状态输出,请查找CGroup之后的第二个命令行,它应该相当长,并从/usr/lib/postgresql/13/bin/postgres或类似的开始(取决于版本,发行版和安装方法)。您正在查找-D之后的目录,例如/var/lib/postgresql/13/main。
添加以下行:host all all 127.0.0.1/32 trust。这允许所有数据库上的所有用户通过本地机器无条件的,不需要密码上的IPv4连接到数据库。
这是一个临时修复,不要忘记稍后再次删除这一行。为了确定,我注释掉了host all all 127.0.0.1/32 md5(md5可能被scram-sha-256替换),它对相同的登录数据有效,只需要密码。