简体   繁体   English

如何在一个命令中为PostgreSQL数据库设置新用户,密码和数据库

[英]How can I setup a new user, password and database all in one command for my PostgreSQL database

How can I setup a new user, password and database all in one command for my PostgreSQL database? 如何在一个命令中为PostgreSQL数据库设置新用户,密码和数据库? I have a test script that needs to run a command to setup a database. 我有一个测试脚本,需要运行命令来设置数据库。 This is what I have tried: 这是我尝试过的:

psql -h localhost -U root

but this asks me to enter a password, is there a way to do it on one command ie 但这要求我输入密码,有没有办法在一个命令上输入密码,即

psql -h localhost -U root -P '123' - database

Where the password and new database is created? 在哪里创建密码和新数据库?

尝试这样的事情:

psql -U postgres -c "CREATE ROLE testuser PASSWORD 'secret' LOGIN"; psql -U postgres -c "CREATE DATABASE test"

I presume you have sudo rights for machine/instance in question or you can connect as postgres. 我认为您对相关机器/实例具有sudo权限,或者您可以作为postgres连接。 Command mentioned by Laurenz Albe will work only if you work as postgres and peer authentication is allowed for user postgres in pg_hba.conf file. Laurenz Albe提到的命令仅在您以postgres身份工作并且pg_hba.conf文件中的用户postgres允许对等身份验证时才有效。

If you are not connected as postgres you must switch to that user - under root su postgres under other user sudo su postgres . 下根-如果你没有连接在Postgres必须切换到该用户su postgres下的其他用户sudo su postgres Then command mentioned above will work. 然后上述命令将起作用。

暂无
暂无

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

相关问题 如何在没有外壳(命令行)访问的情况下将表从一个数据库复制到同一服务器上的另一个数据库? - How can I copy a table from one database to another on the same server without shell (command line) access? 我如何通过学说导入我的数据库 - how can i importing my database by doctrine 无法在PostgreSQL中导入数据库 - Can not import database in postgresql 如何在数据库列表中添加新的用户名和密码(我有一个pass文件和一个用户名文件 - How do I add a new username and password to a database list (i have a pass file an a username file 无法从容器连接到我的Docker主机Postgresql数据库 - Can't connect to my docker host postgresql database from a container 如何在 Linux *不*对 Postgres 用户进行 Sudo-ing 时运行 PostgreSQL 的“dropdb”命令? - How Can I Run PostgreSQL's “dropdb” Command on Linux *Without* Sudo-ing to the Postgres User? 为什么在Linux MSSQL上我不能在一行中输入我的用户、密码和服务器,但我可以单独输入? - Why can't i type my user, password and server in one line on Linux MSSQL , but I can type it separately? 如何在一行命令中将密码应用于 sudo 并执行 su root? - How can I apply password to sudo in one line command and execute su root? 如何向我的本地用户授予所有postgres权限? - How can I grant all postgres permissions to my local user? 如何编写sqoop数据库命令? - How do I write sqoop database command?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM