Error saving psql history file - postgresql

My current psql version is 10.1,
but when I type \q to save the history it shows:
postgres=# \q could not save history to file "/opt/PostgreSQL/9.3/.psql_history": No such file or directory
But I am using Postgres 10.1, how to fix this?

How psql determines the path of the history file is documented as:
HISTFILE
The file name that will be used to store the history list. If unset, the file name is taken from the PSQL_HISTORY environment
variable. If that is not set either, the default is ~/.psql_history,
or %APPDATA%\postgresql\psql_history on Windows
You must be aware thatpsql does not use HOME to figure out the home directory represented by the tidle character, it uses /etc/passwd.
So presumably in the question psql is launched by the postgres user, and when this user was created, it was to install PostgreSQL 9.3 on this machine, and it was not changed afterwards when /opt/PostgreSQL/9.3/ got deleted, so the entry in /etc/passwd still points to that non-existing directory.
This answer on DBA.se give the shell command to fix that:
sudo usermod --home '/path/to/database' postgres

Related

'Not valid archive' when using pg_restore

As someone who is very new to working with databases/SQL I'm having trouble setting up a dump from the database Qscored, which can be found at https://zenodo.org/record/4468361#.YgTTZ-7ML0p, the first dump (ab) is the one I've worked with. The downloaded file is of type POSIX tar archive. According to the README file, these are the commands to be run in a terminal window.
cat qscored_dump_25Jan2021ab > qscored_dump_25Jan2021.tar
psql -U postgres
When in a PostGreSQL user run
CREATE DATABASE qscoreddb WITH TEMPLATE=template0 ENCODING='UTF-8';
CREATE USER dbwala WITH PASSWORD 'password';
GRANT ALL PRIVILEGES ON DATABASE qscoreddb TO dbwala;
Then, in a new terminal window I run pg_restore according to
pg_restore -d qscoreddb -U postgres \path\to\qscoreddb_dump_Jan252021.tar
I then get the error message
pg_restore: error: input file does not appear to be a valid archive
Does anyone know how I might be able to solve this problem or does it mean there are problems in the original file?
I'm using PostGreSQL 14 with a Mac Monterey 12.2.

How to print current working directory in psql console?

I am trying to restore a schema on a remote server using the psql console in pgAdmin and an sql dump file. I receive the following error:
user=> \i file.sql
file.sql: No such file or directory
I can't seem to print the directory listings due to lacking superuser privileges.
Is there a way to identify or print the current working directory in psql console? What is the default directory?
Typing in the command \! pwd will print the current working directory in psql.
To change working directory use command \cd <path>; replace <path> with desired path. Eg. Running command \cd /run/mount will change the current working directory to /run/mount. Try changing the your working directory to that containing the file which you want to run and then use \i meta command as you did earlier. It will definitely work.
I am shocked that no one answered the question directly in over 2 years. Everyone assumes that because you are using postgres that you must be running Linux. Well, postgres is now becoming a very popular DBMS on Windows 10.
In Windows 10, in a psql command prompt type "! dir" to print the current working directory.
To change directories in the Windows 10 psql client, "\cd /users/yourlogin".
The psql client is a unix shell running on Windows, so it is a mix of unix and dos syntax commands.

Set initdb and pg_ctl

I installed Postgresql from source.
I tried running the commands:
which initdb
which pg_ctl
but I get a blank response.
I know where these executables reside in my directory.
How might I set initdb and pg_ctl?
Thanks for your help.
You have received the blank output for command 'which', as these binaries are not known to your linux machine. And hence you will also need to run these binaries using './' (like ./pg_ctl)
You can add the path of your postgres bin directory to $PATH.
Eg.
export PATH=$PATH:/Postgres/Installation/Path/bin/
You can also set it permanently by adding the above path value in .bash_profile file in home of directory of Postgres user.

pgrouting error when trying to dump data into database

I was just following this tutorial HERE, its about, pgrouting, When I run the following command:
psql -U user -d postgres -f ~/Desktop/pgrouting-workshop/data/sampledata_routing.sql
I get an error saying the following:
/var/lib/postgresql/Desktop/pgrouting-workshop/data/sampledata_routing.sql: No such file or directory
On my desktop I do have a folder pgrouting-workshop, which does contain the folder data and the sql dump file.
So why am I getting this error?
Because your Desktop isn't in the postgres user's home directory, located at /var/lib/postgresql, but is instead located at /home/myusername/Desktop?
Presumably the psql command you're running is under a sudo -u postgres -i shell, so ~/ means the postgres user's home directory.
Use ~myusername/Desktop/blahblah. Note that the postgres user may not have permission to access it; you can chmod go+x ~ ~/Desktop (run as your user, not postgres) to change that.

postgreSQL permission denied when reading from file with \i command

this is my problem:
myname#ubuntu:~$ sudo su postgres -c "psql template1"
Password:
psql (9.1.6)
Type "help" for help.
template1=# \i /create.sql
/create.sql: Permission denied
I have this problem even when the file is on the Desktop.
when I copy the text of create.sql and paste it there it works.
using UBUNTU 12.10, postgresql 9.1
Thank you for the help.
If you work in windows , you just need to pass the entire path wrapped by a single quotation.
test-# \i 'D:\\person.sql' --- > note here double backslash not single
The problem is that you've launched psql as the user postgres and you haven't granted the postgres user permission to read the SQL file. You'll need to give it permission. The simplest way is to grant world read rights:
chmod a+r create.sql
You can change the default permissions assigned to files by altering your umask; search for more information. Some programs rather annoyingly ignore the umask and set restrictive file permissions anyway, so you always need to know how to grant permissions. See man chmod and man chown.
BTW, you're using a very convoluted method for launching psql as the postgres user. This'll do just fine:
sudo -u postgres psql template1
Note that /create.sql specifies a file named create.sql in root of the file system (/). I doubt this is what you intended.
You probably want to specify it as a relative path (without the leading /) if it's in your home directory, like:
template1=# \i create.sql
or if it's on the desktop and you've started psql in your home directory:
template1=# \i Desktop/create.sql
EDIT: A better solution is to move the SQL file to the tmp folder if you are on linux!
I ran into this same issue on Linux but the accepted solution was not sufficient in my case. I eventually realized that you need to make sure that EVERY folder in the path has the correct permissions.
For example if the home folder does not have the correct permissions it does not matter what permissions you give a file inside of the SQL folder.
/home/username/Documents/SQL
if you are facing same issue after putting quote then use forward slash for paths