What should the flow be for 'psql' command? Windows command line - postgresql

I can access psql with 'psql -U postgres' followed by the password, but I can't access my defined user with the same 'psql -U definedUser' it just says 'definedUser isn't a database'.
Just to clarify.
How do I log in as a predefined user?
How do I quickly access psql from the command line?

Okay, I think I've figured it out. This comment is what swung it.. Very succinct and easy to solve. If the user has been created successfully, by default when you log in with said user psql attempts to find a database of the same name if it doesn't you get an error like 'desiredUsername isn't a database'.
You need to add the database name after the chosen username
psql -U desiredUsername desiredDatabase
https://stackoverflow.com/a/21827460/15592981

Related

Postges command exection without password

I want to use rundeck to allow app user to reset their own password without admin access.
So I would like to execute this type of command:
export PASSFILE=/usr/pgsql-13/.pgpass ; /usr/pgsql-13/bin/psql -h hostname -U postgres -c """alter user ${job.username} PASSWORD '${option.Password}';"""
But the script fails because postgres ask for password
Any one have a workaround? The postgres DB is not a classic DB but aws aurora DB, so I don't have access to pg_hba file
Many thanks
If you want to use interactive commands on Rundeck you can use expect command, in that way you can "wrap" your command to "answer" any interactive command "question", that was answered here.
The environment valuable is called PGPASSFILE.
An alternative and simpler way is to specify the passfile connection parameter:
/usr/pgsql-13/bin/psql 'host=hostname user=postgres passfile=/usr/pgsql-13/.pgpass' -c "alter user ${job.username} PASSWORD '${option.Password}';"
You can also use the password parameter to specify the password in the connect string.

How to clear PostgreSQL psql command history

When login to the database using psql -u postgres, all the commands entered there can be seen in and recalled from history.
I created a role with a password and I would like to clear that entry.
How can I clear the history?
Please check on the /home/user/.psql_history, then open the file if we want we can clear all the commands or the necessary commands and save the file.
Because you used -u postgres, the file should be in the home of your postgres user. In my case it is /var/lib/postgresql/.psql_history.

Export everything from PostgreSQL

I want to export everything(users, roles, object definitions, data) from my PostgreSQL and restore it into another server. How can I do that?
I try to use
pg_dumpall -U postgres -g > out.sql
but file out.sql is empty. Can someone explain me why this happens?
When I try:
pg_dumpall -U postgres > out.sql
PostgreSQL just didn't want to accept password for "postgres".
I prefer to do this job from (windows) command line(it is not problem and with linux command, I can translate them). And if there is nice pgAdmin way to do it, I will happy to learn it, too.
P.S: My password for "postgres" is correct (I try to login with "psql -U postgres" and there is no problem)
Update: I try with user different than "postgres"(in this case rsmn):
pg_dumpall -U rsmn > out.sql
I have following error:
pg_dumpall: query failed: ERROR: permission dneied for relation
pg_authid pg_dumpall: query was: SELECT oid, rolname, rolsuper,
rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, rolconnlimit,
rolpassword, rolvalidunil, rolreplication, rolbypassrls,
pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, rolname
= current_user as is_current_user FROM pg_authid WHERE rolname !~ '^pg_' ORDER BY 2
User privileges(This user is only one beside "postgres"):
The attempt with user rsmn fails because it is not a superuser and cannot export the user passwords.
It remains to figure out why pg_dumpall -U postgres fails for you.
For that, the first thing would be to check the PostgreSQL server log. I am not sure where that is on your system, you should check the settings in postgresql.conf (logging_collector, log_destination, log_directory, log_filename). Probably it is in the Windows Event Log.
The log message will tell you what is wrong.
It could well be a problem with the pg_hba.conf file – if so, modern PostgreSQL versions will tell you which line was used.
For debugging purposes, you can try to change the relevant line to trust authentication so that no password is requested (but don't leave it that way, or everybody can connect as user postgres without password!).
If you are trying to do it under the *nix and your server is installed localy, you may not to use ident mode instead of password:
sudo su postgres
pg_dumpall > out.sql

.pgpass with AWS RDS

I need to run multiple commands on an AWS Postgres RDS instance I have. I don't want to enter the password each time. I'm trying to use the .pgpass file but I'm running into errors. The first time I ran into an error which said 'role "ubuntu username" does not exist'. I logged in as the postgres user and created that username. After this, the error I get said database does not exist. I have a feeling these errors have nothing to do with trying to connect to the AWS RDS instance.
psql --host=<awshost> --port=5432 --username=<awsrdsusername> --password --dbname=<dbname asks for a password and then logs me in after I enter it. Now I put a file in /home/<ubuntuusername> called .pgpass which has <awshost>:5432:<dbname>:<awsrdsusername>:<password>. Permissions for this file are set to 0600. Now when I run psql from the terminal and that produces the error - psql: FATAL: role "<ubuntuusername>" does not exist. These steps are as outlined on this page.
Can someone help me with the steps to get a pgpass file to connect to an AWS RDS instance?
.pgpass doesn't provide connection information. You seem to expect that after you create a .pgpass file, you can run psql without arguments and it'll know where to connect. That is not the case.
The hostname, port, etc you put in .pgpass are there so that PostgreSQL knows which line to look at when matching the connection info to find its password.
If you had to run:
psql -h something
to connect without a .pgpass file, you still have to run the same thing to connect with a .pgpass file.
If you run psql without arguments it'll connect to the local PostgreSQL (if any), using the current unix username as the postgresql username and the database to connect to. That's why you get the error you do.
If you want to change the default connection, you can use environment variables like PGHOST, PGPORT, etc, and/or a .pgservice.conf file.
See the manual to learn more.

PostgreSQL 9.4, 64-bit windows: Unable to create user from commandline

I'm trying to create a user from command line using PostgreSQL version 9.4 on a 64 bit machine with windows.
I'm using the below command to create a new user:
createuser -d temba
On executing the above command, it prompts me for a password.
I enter the password (qwerty) which i used while installing PostgreSQL. on doing so, i get the below error:
psql: FATAL: password authentication failed for user "my-windows-user-name"
Next, i tried giving my login password for windows, i get the same error as above.
Can anyone guide me through the procedure for creating a new user from command line (only, I'm not allowed to use PgAdmin to create user).
I have checked previous posts with similar errors. But the problem there is, either they are not using windows or they are using a really old version of PostgreSql.
Any information on how to proceed about with this shall be really helpful.
Thanks in advance.
All Postgres command line tools try to connect to the database using the current operating system user if no database user is specified.
So you need to specify the user name of the Postgres superuser that is used to connect to the Postgres server:
createuser -U postgres -d temba
This becomes more evident if you use psql instead. You specify the Postgres user account and the target database when you start it:
psql -U postgres -d temba
Then at the prompt you can run create user ....
temba=# create user foobar ... ;
Which is what the command line tool createuser is doing in the background.