In postgresql: CREATE ROLE works but createuser doesn't

时间:2019-01-09 22:17:52

标签: database postgresql

I'm new to PostgreSQL and was following this tutorial. I can create roles just fine but when I tried to use the createuser and dropuser commands, it just doesn't do anything and no new users are created or any deleted. I tried to use it with and without the semi colon at the end, the former gives a syntax error and the latter just doesn't do anything.

    postgres-# createuser joe;
ERROR:  syntax error at or near "createuser"
LINE 1: createuser 
        ^
postgres=# ;
postgres=# createuser joe;
ERROR:  syntax error at or near "createuser"
LINE 1: createuser joe;
        ^
postgres=# createuser joe
postgres-# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 admin     | Superuser, Create DB                                       | {}
 john      | Superuser, Create role, Create DB, Replication             | {}
 guest     |                                                            | {}
 guest3    |                                                            | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

I also tried this:

createuser --interactive joe 

This also didn't do anything.

What's the correct way to use createuser? I'm using the following version.

postgres (PostgreSQL) 11.1

3 个答案:

答案 0 :(得分:3)

Inside the psql tool you need to enter SQL commands. To create a user from SQL, you need to use create user.

The tutorial probably was running the command line utility createuser (not the SQL command)

To understand why:

postgres=# createuser joe

did not do anything, see: In psql, why do some commands have no effect?

答案 1 :(得分:1)

I think you need a space between your command, something like the following:

CREATE USER youruser WITH ENCRYPTED PASSWORD 'yourpass';
GRANT ALL PRIVILEGES ON DATABASE yourdbname TO youruser;

答案 2 :(得分:0)

命令createuser需要在console(bash)中运行。无需在psql中执行此操作。 示例:

createuser -h localhost -p 5432 joe