Create a user with a password
This will create a PostgreSQL user with niceusername
username and very-strong-password
password.
CREATE USER niceusername with PASSWORD 'very-strong-password';
Although this method is simple, it lets the clear password in the client history file. If using psql, it is advised to use the \password
command to do that. If the user issuing the command is a superuser, the current password will not be asked.
CREATE USER niceusername with LOGIN;
\password niceusername
Create user and matching database
To support a given application, you often create a new user and database to match.
The shell commands to run would be these:
$ createuser -P blogger
Enter password for the new role: ⚫⚫⚫⚫⚫⚫⚫⚫
Enter it again: ⚫⚫⚫⚫⚫⚫⚫⚫
$ createdb -O blogger blogger
This assumes that pg_hba.conf
has been properly configured, which probably looks like this:
# TYPE DATABASE USER ADDRESS METHOD
host sameuser all localhost md5
local sameuser all md5