简体   繁体   English

AWS EC2 用户数据脚本:如何创建 PostGres 用户、密码和数据库?

[英]AWS EC2 userdata script: How create PostGres User,Password and DataBase?

I am trying to create Postgres user, passWord and database using userdata field in AWS EC2.我正在尝试使用 AWS EC2 中的用户数据字段创建 Postgres 用户、密码和数据库。

My last attempt was:我最后的尝试是:

sudo su postgres
psql -U postgres -c "CREATE ROLE postgre;"
psql -U postgres -c "ALTER ROLE  postgre  WITH LOGIN;"
psql -U postgres -c "ALTER USER  postgre  CREATEDB;"
psql -U postgres -c "ALTER USER  postgre  WITH ENCRYPTED PASSWORD '1234567';"
psql -U postgres -c "create database productdb';"

I got this error message for each 5 last lines above:对于上面的最后 5 行,我收到此错误消息:

psql: error: FATAL: Peer authentication failed for user "postgres" psql:错误:致命:用户“postgres”的对等身份验证失败

Any suggestion to create user, password and database using userdata field in EC2?在 EC2 中使用用户数据字段创建用户、密码和数据库有什么建议吗?

I found this link , but I dont know how use this solution in EC2 userdata script.我找到了这个链接,但我不知道如何在 EC2 用户数据脚本中使用这个解决方案。

You don't need sudo su because userdata script is already run as root.您不需要sudo su因为 userdata 脚本已经以 root 身份运行。 So you can become another user with just one of those commands, you don't need both.因此,您只需使用这些命令中的一个就可以成为另一个用户,您不需要同时使用这两个命令。

There are several ways to do it, but this is how I generally do:有几种方法可以做到这一点,但这是我通常的做法:

su - postgres <<'EOpostgres'
psql -U postgres -c "CREATE ROLE postgre;"
psql -U postgres -c "ALTER ROLE  postgre  WITH LOGIN;"
psql -U postgres -c "ALTER USER  postgre  CREATEDB;"
psql -U postgres -c "ALTER USER  postgre  WITH ENCRYPTED PASSWORD '1234567';"
psql -U postgres -c "create database productdb';"
EOpostgres

Note that all of those -U postgres are redundant, as they just specify what is already the default.请注意,所有这些-U postgres都是多余的,因为它们只是指定已经是默认值的内容。

I use this in the userdata script on ubuntu. I assume it or something like it will work with amazon-linux, but I've already touched that hot stove once and don't want to try it again.我在 ubuntu 上的用户数据脚本中使用了它。我假设它或类似的东西可以与 amazon-linux 一起使用,但我已经接触过那个热炉一次,不想再试一次。

Peer authentication needs hostname in the psql parameters.对等身份验证需要 psql 参数中的主机名。 Try adding -h localhost or host IP in the psql command.尝试在 psql 命令中添加-h localhost或 host IP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM