This page in other versions: 8.4 / 9.0 / 9.1 / 9.2 / 9.3  |  Development versions: devel  |  Unsupported versions: 7.1 / 7.2 / 7.3 / 7.4 / 8.0 / 8.1 / 8.2 / 8.3

CREATE USER

Name

CREATE USER -- define a new database user account

Synopsis

CREATE USER name [ [ WITH ] option [ ... ] ]

where option can be:
    
      SYSID uid 
    | CREATEDB | NOCREATEDB
    | CREATEUSER | NOCREATEUSER
    | IN GROUP groupname [, ...]
    | [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password'
    | VALID UNTIL 'abstime' 

Description

CREATE USER adds a new user to a PostgreSQL database cluster. Refer to Chapter 17 and Chapter 19 for information about managing users and authentication. You must be a database superuser to use this command.

Parameters

name

The name of the new user.

uid

The SYSID clause can be used to choose the PostgreSQL user ID of the new user. This is normally not necessary, but may be useful if you need to recreate the owner of an orphaned object.

If this is not specified, the highest assigned user ID plus one (with a minimum of 100) will be used as default.

CREATEDB
NOCREATEDB

These clauses define a user's ability to create databases. If CREATEDB is specified, the user being defined will be allowed to create his own databases. Using NOCREATEDB will deny a user the ability to create databases. If not specified, NOCREATEDB is the default.

CREATEUSER
NOCREATEUSER

These clauses determine whether a user will be permitted to create new users himself. CREATEUSER will also make the user a superuser, who can override all access restrictions. If not specified, NOCREATEUSER is the default.

groupname

A name of an existing group into which to insert the user as a new member. Multiple group names may be listed.

password

Sets the user's password. If you do not plan to use password authentication you can omit this option, but then the user won't be able to connect if you decide to switch to password authentication. The password can be set or changed later, using ALTER USER.

ENCRYPTED
UNENCRYPTED

These key words control whether the password is stored encrypted in the system catalogs. (If neither is specified, the default behavior is determined by the configuration parameter password_encryption.) If the presented password string is already in MD5-encrypted format, then it is stored encrypted as-is, regardless of whether ENCRYPTED or UNENCRYPTED is specified (since the system cannot decrypt the specified encrypted password string). This allows reloading of encrypted passwords during dump/restore.

Note that older clients may lack support for the MD5 authentication mechanism that is needed to work with passwords that are stored encrypted.

abstime

The VALID UNTIL clause sets an absolute time after which the user's password is no longer valid. If this clause is omitted the password will be valid for all time.

Notes

Use ALTER USER to change the attributes of a user, and DROP USER to remove a user. Use ALTER GROUP to add the user to groups or remove the user from groups.

PostgreSQL includes a program createuser that has the same functionality as CREATE USER (in fact, it calls this command) but can be run from the command shell.

The VALID UNTIL clause defines an expiration time for a password only, not for the user account per se. In particular, the expiration time is not enforced when logging in using a non-password-based authentication method.

Examples

Create a user with no password:

CREATE USER jonathan;

Create a user with a password:

CREATE USER davide WITH PASSWORD 'jw8s0F4';

Create a user with a password that is valid until the end of 2004. After one second has ticked in 2005, the password is no longer valid.

CREATE USER miriam WITH PASSWORD 'jw8s0F4' VALID UNTIL '2005-01-01';

Create an account where the user can create databases:

CREATE USER manuel WITH PASSWORD 'jw8s0F4' CREATEDB;

Compatibility

The CREATE USER statement is a PostgreSQL extension. The SQL standard leaves the definition of users to the implementation.

Comments


April 4, 2005, 6:18 p.m.

One thing that was unclear to me was how to create PostgreSQL MD5 hashes. I wanted to store the scripts that generate user accounts without having the plaintext password in the file. The documentation is a little ambiguous on what a correctly "MD5-encrypted format" password string is. To generate a correct MD5 string for PostgreSQL, concatenate the password and username together and pass that into an MD5 generator. Then take that hash a prepend the string 'md5' to it.

Using psql do:
SELECT md5('passwordusername');
That will be the correct hash so then just put 'md5' in front of that. I also created a perl script to this too.

#!/usr/bin/perl
use strict;
use warnings;
use Digest::MD5;

# This script generates a PostgreSQL MD5 hash for a user's password.
my ($password, $username, $md5);

if ($#ARGV + 1 == 2) {
$username = $ARGV[0]; # postgresql username
$password = $ARGV[1]; # plaintext password
} else {
print "Usage: perl pgmd5.pl username password\n";
print "username = postgres username";
print "password = plaintext password\n";
exit(0);
}

$md5 = Digest::MD5->new;

#postgres passwords in the pg_shadow table are generated by using the plaintext password and the username
$md5->add($password . $username);

# the text 'md5' plus the previous md5 hash of the password and usename are what's stored in the pg_shadow table
print "md5" . $md5->hexdigest . "\n";

The user testuser with a password of testpassword would have a hash of md5d4fc5129cc2c25465a5370113ae9835f.

The SQL script would be:
CREATE USER testuser NOCREATEUSER NOCREATEDB WITH ENCRYPTED PASSWORD 'md5d4fc5129cc2c25465a5370113ae9835f';


Oct. 20, 2006, 5:17 p.m.

Here's a java version of the same code:

public void updatePassword(String password) {
String algorithm = "MD5";
String queryForDebug = null;

MessageDigest md = MessageDigest.getInstance(algorithm);
md.update((password + getUsername()).getBytes());


String md5hashed = "md5" + ByteUtils.toHexString(md.digest()).toLowerCase();

StringBuilder query = new StringBuilder();
query.append("alter user ").append(getUsername()).append(" with encrypted password '").append(md5hashed).append("'");
System.out.println("Query: " + query.toString());


}

Here's the code to make it a hex string:
/**
* Convert a byte[] array to readable string format. This makes the "hex"
readable!
* @return result String buffer in String format
* @param in byte[] buffer to convert to string format
*/

public static String toHexString(byte in[]) {
byte ch = 0x00;
int i = 0;

if (in == null || in.length <= 0)
return null;

String pseudo[] = {"0", "1", "2","3", "4", "5", "6", "7", "8","9", "A", "B", "C", "D", "E","F"};
StringBuffer out = new StringBuffer(in.length * 2);
while (i < in.length) {
ch = (byte) (in[i] & 0xF0); // Strip off high nibble
ch = (byte) (ch >>> 4); // shift the bits down
ch = (byte) (ch & 0x0F); // must do this is high order bit is on!
out.append(pseudo[ (int) ch]); // convert the nibble to a String Character
ch = (byte) (in[i] & 0x0F); // Strip off low nibble
out.append(pseudo[ (int) ch]); // convert the nibble to a String Character
i++;
}
String rslt = new String(out);
return rslt;

}

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group