psql: FATAL: role "user" does not exist - postgresql

My psql does not open. Weirdly enough. I've created a user "Ben" through logging using psql -U postgres that lets me open PSQL and I run a command
CREATE USER Ben WITH PASSWORD '123';
After I do that it says ERROR: role "ben" already exists
But when I run psql it throws me psql: FATAL: role "Ben" does not exist

When you issue an SQL command like this:
CREATE USER Ben WITH PASSWORD '123';
identifiers within this command (such as the user name) are folded to lower case. So the user is actually created as 'ben'.
When you issue a shell command such as:
psql -U Ben mydatabase
then the identifier case is preserved, making it case sensitive.
If you really want the user name capitalized, then double quote it in SQL, like this:
CREATE USER "Ben" WITH PASSWORD '123';
Otherwise leave it all lower case and connect with:
psql -U ben mydatabase
See: Identifiers & Keywords in the manual

Related

How do I make postgres user as the main user instead of typing psql -U postgres?

I install my psql 12.4 using this: https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
When the system prompt me to enter password for user "postgres". I did give it a password.
But when I type psql on powershell terminal, it will require me to give password for user JinTan which i do not have.
(base) PS C:\Users\JinTan> psql
Password for user JinTan:
The only password i have is this:
(base) PS C:\Users\JinTan> psql -U postgres
Password for user postgres:
psql (12.4)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
postgres=#
So my question is:
How do I make postgres user as the main user instead of typing psql -U postgres?
Thanks all for the advise. I have solved it using all the advise.
I use 'a_horse_with_no_name' method to add PGUSER with value of 'postgres' in my env variable.
To not allow system to ask for password everytime,
I use "Belayer" method to change all of the "md5" value in pg_hba.conf to "trust".
Then it works like charm without needing me to enter password everytime.

How to create user in postresql version 12?

I have loged in postresql as default postgres user:
psql -U postres
and following the official documentation: https://www.postgresql.org/docs/12/app-createuser.html I have type this:
create user -d -e --role=myrole -r -s myuser;
and nothing happens. Postresql ignores the command.
\du
does not return myuser in user list.
It ignores the command when added any option, only this works:
create user myuser;
CREATE USER is a SQL statement that has its own syntax https://www.postgresql.org/docs/12/sql-createuser.html whereas createuser is an executable that has a different syntax: https://www.postgresql.org/docs/12/app-createuser.html: you cannot mix them.

Postgres: Create role in one line

I want to create a role/user in one line. Here's what I tried:
psql -U postgres -c 'createuser my_app with createdb login password 'my_password';'
psql: warning: extra command-line argument "with" ignored
psql: warning: extra command-line argument "createdb" ignored
psql: warning: extra command-line argument "login" ignored
psql: warning: extra command-line argument "password" ignored
psql: warning: extra command-line argument "'my_password';'" ignored
psql: FATAL: database "my_app" does not exist
In place of createuser I have also tried create user and create role, but regardless, I get the same error. I'm on Windows with (PostgreSQL) 9.6.2
What am I doing wrong?
Update:
I tried using double quotes, but for some reason, postgres doesn't seem to like my double quotes. Using double quotes inside of single quotes mysteriously works -> Thanks #Laurenz
psql -U postgres -c "createuser my_app with createdb login password 'my_password';"
ERROR: syntax error at or near "createuser"
LINE 1: createuser my_app with createdb login password 'my_password'...
createuser is not an SQL command, so that won't work.
CREATE USER is correct, but you can't nest single quotes in single quotes like that.
It should be
psql -U postgres -c "CREATE USER my_app CREATEDB PASSWORD 'my_password'"

Changing psql database name

When I run psql, the user it defaults to is my username:
$ psql
psql (9.4.5)
Type "help" for help.
jeffrey.wan=#
How do I change it to something else?
psql -U jeffrey.wan desired_user
psql (9.4.5)
Type "help" for help.
desired_user=#
Type \? to get the list of the available commands, and you will see that the one you are looking for is:
\c desired_user
By the way, the displayed name is the database you are connected to, not the user you are connected as.
You can specify another user by typing it as second parameter.
But you'd better look up for the command line parameters when you connect the first time. For example, this will bring you where you want directly:
psql -d database -U user -h hostname

How to switch databases in psql?

In MySQL, I used use database_name;
What's the psql equivalent?
In PostgreSQL, you can use the \connect meta-command of the client tool psql:
\connect DBNAME
or in short:
\c DBNAME
You can connect to a database with \c <database> or \connect <database>.
At the PSQL prompt, you can do:
\connect (or \c) dbname
You can select the database when connecting with psql. This is handy when using it from a script:
sudo -u postgres psql -c "CREATE SCHEMA test AUTHORIZATION test;" test
use \c databaseName or \connect databaseName
(Working on psql 13.3)
\l for databases
\c DatabaseName to switch to db
\df for procedures stored in particular database
Though not explicitly stated in the question, the purpose is to connect to a specific schema/database.
Another option is to directly connect to the schema. Example:
sudo -u postgres psql -d my_database_name
Source from man psql:
-d dbname
--dbname=dbname
Specifies the name of the database to connect to. This is equivalent to specifying dbname as the first non-option argument on the command line.
If this parameter contains an = sign or starts with a valid URI prefix (postgresql:// or postgres://), it is treated as a conninfo string. See Section 31.1.1, “Connection Strings”, in the
documentation for more information.
Using psql's meta-command \c or \connect [ dbname [ username ] [ host ] [ port ] ] | conninfo (see documentation).
Example: \c MyDatabase
Note that the \c and \connect meta-commands are case-sensitive.
Use below statement to switch to different databases residing inside
your postgreSQL RDMS
\c databaseName
You can also connect to a database with a different ROLE as follows.
\connect DBNAME ROLENAME;
or
\c DBNAME ROLENAME;
You can connect using
\c dbname
If you would like to see all possible commands for POSTGRESQL or SQL follow this steps :
rails dbconsole
(You will be redirected to your current ENV database)
?
(For POSTGRESQL commands)
or
\h
(For SQL commands)
Press Q to Exit
If you want to switch to a specific database on startup, try
/Applications/Postgres.app/Contents/Versions/9.5/bin/psql vigneshdb;
By default, Postgres runs on the port 5432. If it runs on another, make sure to pass the port in the command line.
/Applications/Postgres.app/Contents/Versions/9.5/bin/psql -p2345 vigneshdb;
By a simple alias, we can make it handy.
Create an alias in your .bashrc or .bash_profile
function psql()
{
db=vigneshdb
if [ "$1" != ""]; then
db=$1
fi
/Applications/Postgres.app/Contents/Versions/9.5/bin/psql -p5432 $1
}
Run psql in command line, it will switch to default database; psql anotherdb, it will switch to the db with the name in argument, on startup.
Listing and Switching Databases in PostgreSQL
When you need to change between databases, you’ll use the \connect command, or \c followed by the database name as shown below:
postgres=# \connect database_name
postgres=# \c database_name
Check the database you are currently connected to.
SELECT current_database();
PostgreSQL List Databases
postgres=# \l
postgres=# \list
Connect to database:
Method 1 : enter to db : sudo -u postgres psql
Connect to db : \c dbname
Method 2 : directly connect to db : sudo -u postgres psql -d my_database_name
You can just enter use [dbName] to switch between databases without reentering your password.