How to clear PostgreSQL psql command history - command-line

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.

Related

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

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

Automating a PG_DUMP of a PSQL database on Heroku

I'm trying to pg_dump certain schemas from my Heroku-hosted PSQL database into a local file.
Heroku provides me a DATABASE_URL in the form
# postgres://username:password#host:port/database
postgres://abcde:wxyz#ec2-21-82-72-112.compute-1.amazonaws.com:5762/riza3dj029012
Based on the above I tried dumping some schemas -
> pg_dump --username=abcde --host=ec2-21-82-72-112.compute-1.amazonaws.com --port=5762 --dbname=riza3dj029012 --create --schema=my_schema --password > ~/pg_dump.dmp
> password: (enter password)
Is there any way to provide the password as a flag so I don't have to type it in manually? I want to automate this in a script.
I know --no-password exists, but not sure what use that is, because it just prevents prompting for a password and then authentication (obviously) fails.
Thanks!
As #eabates mentioned in the comments you can create a .pgpass file for this purpose. More info can be found in the official documentation for Postgres: https://www.postgresql.org/docs/9.5/static/libpq-pgpass.html
Just create a file named .pgpass in the user home directory with as many lines as needed in the following format:
hostname:port:database:username:password

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.

psql: FATAL: database "Morne Nel" does not exist

I need to do a report in JasperReports.... besides the point here.
Ive been given a existing Postgresql DB to use.
I have installed PostgreSQL and all went great. (except the package update at the end. Cant get past the proxy server)
Ive added the C:\Program Files\PostgreSQL\9.0\bin path the the system path as-well.
When I go to cmd and type psql it propts for a password....
I enter password, because thats the only password I added during instalation.
Then is comes up with psql: FATAL: database "Morne Nel" does not exist
step by step hoe do I import this DB into postgres? PLEASE?
When using psql you have to pass database name, otherwise postgres will try to connect to database matching your user name (that is why it tries to connect to "Morne Nel").
You can use createdb console command to create database first. Here is documentation on that command: http://www.postgresql.org/docs/9.3/static/sql-createdatabase.html
But also you can use pg_restore command right away with --create option passed to create and restore database from dump at once (documentation here: http://www.postgresql.org/docs/9.3/static/app-pgrestore.html).
Try to execute those commands (it is possible that you will have to specify your postgres user and password) and let us know what happened. I cannot write more at the moment as I am not sure what dump format do you have so it would be good to provide that information if you won't manage to use pg_restore successfully.

How to use pg_dump / pg_dumpall with postgres DB?

I am uncertain if its the permissions issue or something I am doing wrong and any help will be greatly appreciated.
I have a dedicated Ubuntu server with Postgres DB installed on it. I want to backup the databases, but when I use pg_dump I get permission denied. Can some one help me out?
Here are a few details about my set up.
On postgres: I have the following databases.
postgres,
template0,
template1,
mydb,
mydb1
I have 2 PostgreSQL users
postgres,
mydb (There are no postgresql groups defined yet)
All the other settings for postgres are default values.
On my ubuntu itself, I have 2 users.
user1,
mydb
I logged into the box as root/user1 using SSH, issued the following commands,
$ pg_dumpall > a.sql
I get, -bash: a.sql: Permission denied
Same goes with pg_dump command.
I know I am missing something here. Can some one guide me to properly issue this command?
TIA.
I get, -bash: a.sql: Permission denied
Same goes with pg_dump command.
This looks more like a file permission issue. Does the OS user have the right to write the dump file? Check file/directory permissions.
When you run the command
pg_dumpall > a.sql
then your shell will attempt to create the file a.sql in the current directory. If you (as "user1") do not have permission to create files in the current directory, then first use the cd command to change to a directory where you do have permission. You can use the pwd command to see what the current directory actually is.