postgresqlHow do I create a user in PostgreSQL?
To create a user in PostgreSQL, you must have the necessary privileges to do so.
First, connect to the database server using the psql
command:
$ psql -h localhost -U postgres
Then, create a new role with the CREATE ROLE
command:
postgres=# CREATE ROLE myuser WITH LOGIN PASSWORD 'mypassword';
CREATE ROLE
You can also specify additional options such as the user's privileges, the default database, and the expiration date.
postgres=# CREATE ROLE myuser WITH LOGIN PASSWORD 'mypassword' CREATEDB CREATEROLE NOCREATEUSER VALID UNTIL '2021-01-01';
CREATE ROLE
The following list explains the meaning of each option used in the example above:
LOGIN
: The user will be able to authenticate with the given password.CREATEDB
: The user will be able to create new databases.CREATEROLE
: The user will be able to create new roles.NOCREATEUSER
: The user will not be able to create new users.VALID UNTIL
: The user's account will expire on the given date.
Finally, you can verify that the user was created successfully by using the \du
command:
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
myuser | Create DB | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
For more information about creating users in PostgreSQL, you can refer to the official documentation.
More of Postgresql
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How can I use PostgreSQL with YAML?
- How can I use PostgreSQL XOR to compare two values?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How do I parse XML data using PostgreSQL?
- How do I set the PostgreSQL work_mem parameter?
- How do I use a PostgreSQL XML parser in an example?
- How can I convert XML data to a PostgreSQL table?
- How do I set up a web interface for PostgreSQL?
See more codes...