34

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.

1
  • 4
    Looking back six years later, I would probably not attempt this. Installing Postgres on dev machines is a pain, just use the standard Postgres/Docker image: hub.docker.com/_/postgres Commented Apr 4, 2017 at 12:15

5 Answers 5

58

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 https://www.postgresql.org/docs/current/sql-createrole.html for information on the CREATE ROLE options.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 Thank you very much. I looked everywhere but this is the only clear , concise, and working answer I've found ;-)
Is there any difference between login role and ordinary user like postgres ?
5

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}

Comments

2

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).

Comments

1

This worked for me --username Shweta;

Now to create a database create database db;

Comments

1

The simplest case is to use the Windows command line and as I understand that the user posgres is present:

psql -U postgres -c "CREATE ROLE shpp;"

psql -U postgres -c "ALTER ROLE  shpp WITH LOGIN;"

psql -U postgres -c "ALTER USER  shpp CREATEDB;"

psql -U postgres -c "ALTER USER  shpp WITH PASSWORD 'shpp';"

In my shpp example, this is the name and password of the new user.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.