How can I create a capitalized Postgres role name? - postgresql

I am getting the following error when I try to start my Rails app running a Postgres database:
ActiveRecord::NoDatabaseError
FATAL: role "Divergent" does not exist
To fix this I ran CREATE ROLE Divergent from inside the psql console but it only creates a lowercase divergent.
How then do I create a role name that matches the case of the name that the Postgres db expects me to have (i.e Divergent with a 'D')? Why does the Postgres db expect me to have this name and can I change it?

The problem was in config/database.yml. The username in my development database config was commented out and so postgres was defaulting to using my system username as the default role name. I simply uncommented the line and named it what I wanted. And then in the postgres console I created a role that corresponded to that username. That did the trick.

Quote the user name to have it be case sensitive. Note that you'll need to quote it again whenever you use it inside psql (like GRANT ALL ON table TO "Divergent").
You can also use the createuser command outside of psql, which is case sensitive: createuser Divergent.

Related

How to set default database name when creating a cluster

When creating a cluster using initdb
$ initdb -D /usr/local/pgsql/data
it creates a default database user postgres and database name postgres.
I need to create a cluster with a different superuser name and default database name. I found the way to have a different name for superuser:
$ initdb --username=myuser
But can't find how to define the different name for the database instead of postgres. How can I do that? Is my only option is to rename the default DB after it's been created?
Creating the database "postgres" is hardcoded in the initdb-code. You could build your own version of initdb and use a different name for this specific database or you run a script afterwards that renames this database.

How to reset password in postgresql (psql) for particular role in case when I list the role name it is not exist?

I have installed PostgreSQL for a long time but just currently learning it.
Here is what happened if I run psql in the command prompt
C:\Users\VandaRsq>psql
Password for user Vanda Rashq:
Since I forgot the password for the Vanda Rashq role but I remember for the postgres role, I run psql -U postgres.
I tried to list the role by using du command and the result is this:
I also tried using SELECT rolname FROM pg_roles command and yield:
I have tried to follow this tutorial and do ALTER USER "Vanda Rashq" WITH PASSWORD 'new_password'; but it returns ERROR: role "Vanda Rashq" does not exist
My question is, does the "Vanda Rashq" role actually still exist? If yes, how to reset (change) the password in case I forgot the password? If not, how to change the default role when running psql to postgres role
Notes: I have tried to uninstall the PostgreSQL and remove all of the directories but when I try to run psql, it still ask Password for user Vanda Rashq
If the user you're looking for is not listed after calling \du in psql then the user does not exist in the database.
Btw, you could also use a select to retrieve information about database users: select * from pg_catalog.pg_user;
EDIT:
Like #jjanes pointed out you get challenged for a password based on the USER configuration in yourpg_hba.conf (see docs).
For authentication method peer it is stated:
Obtain the client's operating system user name from the operating system and check if it matches the requested database user name.

postgres login asks for non superuser password

I set up postgres according to the instructions for Windows 10 but every time I try to run psql it asks for a non superuser password which I haven't created. How do I make it ask for the superuser without using psql -U postgres command every time? Or how can I set/change a password for a non superuser? I've tried using ALTER ROLE to change the password but get role [username] does not exist as an error message.
By default, psql tries to use your OS username as a database username. Presumably this role hasn't been created in your database, hence the "does not exist" error.
You can override this default by setting the PGUSER environment variable.

createdb: permission denied to create database

I'm trying to set up a Heroku environment for python development following instructions on https://github.com/heroku/python-getting-started. When I run createdb python_getting_started:
I'm first prompted to give in a password: I entered the password of the user "postgres" in Postgres
I get an error message:
createdb database creation failed: ERROR: permission denied to create database
Don't really how to solve this one. The user "postgres" is allowed to create a database. I checked with \du that it is a Superuser and it has Create DB rights. What's going on here? Which user is Windows using to try to create a Postgres DB?
Most PostgreSQL utilities by default use your current OS session login for database connections.
You need to either set environment variable PGUSER to postgres or use createdb -U postgres python_getting_started.
You can read more about createdb parameters here, tho admittedly it does not mention default values.
EDIT: It actually does mention that it uses libpq defaults, and those are:
user
PostgreSQL user name to connect as. Defaults to be the same as the operating system name of the user running the application.

What is a valid PostgreSQL database name?

I create a database with a hyphen in the middle of the name with createdb. That successfully creates the database, but within the psql interactive client, I get a syntax error if I try a command like this:
ALTER DATABASE my-database SET SCHEMA = myschema,public;
psql complains of a syntax error at or near "-"
Is there some documentation for what counts as a valid PostgreSQL database name?
Should I just underscores instead of hyphens?
The documentation you asked about is here:
http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
Most people just stick to lowercase letters, numeric digits, and underscores -- to avoid typing the quotes all the time.
Try putting it in double quotes:
ALTER DATABASE "my-database" SET SCHEMA = myschema,public;
I faced one issue and above answers helped me. So sharing scenario on dbname
Scenario:I was tried to change database name using PG admin III. My database name was My_Database
running below queries failed:
ALTER DATABASE My_Database RENAME TO dba;
ALTER DATABASE [My_Database] RENAME TO dba;
ALTER DATABASE 'My_Database' RENAME TO dba;
Then i tried below and its successful
ALTER DATABASE "My_Database" RENAME TO dba;