Changing psql database name - postgresql

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

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.

Attempting to run pg_dump as user "postgres" returns error related to non-postgres user

When I try to run pg_dump as the user "postgres",
$ sudo -u postgres pg_dump <pg_dump arguments>
I receive the error
psql: FATAL: role "username" does not exist
where "username" is my OS username (i.e., $USER). It seems like if I were attempting to use pg_dump as myself, it would matter that there's no "username" role, but since I'm not, it shouldn't. How do make pg_dump do something? Postgres 9.4.8 on Fedora. Thanks in advance!
If you are running pg_dump, why does the error message say psql?
I'm guessing you are doing something like this:
sudo -u postgres pg_dump foo bar $(psql blurb blurgle)
or possibly this:
sudo -u postgres pg_dump foo bar | psql blurb blurgle
So you need to sudo -u postgres for both commands.
In any case, it sounds like you are looking in the wrong place for the problem.

What is the role of -s flag in creating user

I am trying to a create a user in postgres, I did the following.
sudo -u postgres createuser mystore
But I found out that I should use -s flag while creating the user, So my question is what is role of -s flag while creating user.
And I tried to remove the user by the following steps
sudo -u postgres psql
drop user mystore
Then tried to create the store with the -s flag, it says
role "mystore" already exists.
How to handle this
createuser -s will give the new user superuser privileges. As with most command-line tools in Linux, you can get a description of each flag by running createuser --help.
The problem in psql appears to be a missing semicolon after your drop command. psql supports multi-line statements, so hitting Enter will simply add a new line; it won't submit the command to the server until it sees a semicolon terminator.

PostgreSQL define user parameter for command line psql.exe

How do I define the user for PostgreSQL on the command line?
If I want to sign in as root I would use the following with MySQL...
mysql -uroot -p
Also is there a way to list users? I'm connected via pgAdmin III so if a list is available there I just haven't found it.
You have to use the U parameter as following:
psql -U name_of_user
Source
Thanks to #a_horse_with_no_name, programs use /? to list parameters by default. The following is the default user without a password (even though the setup process asked for a password and it does not match for the default user on the command prompt but it does in phAdmin III).
psql --username=postgres