简体   繁体   中英

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? 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. 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.

If you are not connected as postgres you must switch to that user - under root su postgres under other user sudo su postgres . Then command mentioned above will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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