How to add a user to PostgreSQL in Windows?

70,036

Solution 1

In pgadmin you can create a new "Login Role" and name it Eric and give it permissions graphically, or from command line you can do something like this

psql -U postgres -c "CREATE ROLE Eric LOGIN NOSUPERUSER INHERIT CREATEDB CREATEROLE;" mydb

see http://developer.postgresql.org/pgdocs/postgres/sql-createrole.html for information on the CREATE ROLE options.

Solution 2

Just to add more information. From official documentation: you can specify the user under which createuser utility logs in to postgres via environment variable:

PGUSER

One liner for powershell:

& { $env:PGUSER="postgres"; .\createuser.exe Eric}

Solution 3

The documentation for createuser indicates that a -U switch is accepted:

-U username
--username username

User name to connect as (not the user name to create).

This is what I would expect to use (although I've never tried to set up PostgreSQL on Windows, only on unices).

Solution 4

This worked for me --username Shweta;

Now to create a database create database db;

Share:
70,036
Eric Wilson
Author by

Eric Wilson

Software developer, experienced in Java and Python in Linux environments, formerly a professor of mathematics. I'm a father of five children, and a husband of one wife.

Updated on September 15, 2020

Comments

  • Eric Wilson
    Eric Wilson over 3 years

    I'm running PostgreSQL on mt Windows 7 machine. To run a database I type:

    C:\psql -Upostgres mydb
    

    and this works, but it would be nice if I could leave off the -U stuff, but then Postgres thinks I'm trying to log in as 'Eric', since that is my user profile.

    So I need to add a user to Postgres, obviously. But how? If I try:

    C:\createuser Eric
    

    Postgres thinks I'm trying to add a user Eric as the user Eric which fails. Adding the -U flag doesn't seem to work here.

    What am I missing? My command window is in administrator mode, and there is no sudo available, obviously.