How to print current working directory in psql console? - postgresql

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.

Related

PSQL -f Parameter Hanging on Windows 8.1

I've been using PSQL 14 on my Windows 10 desktop with Git Bash for a while now without issue. Recently I've had to transition to a Windows 8.1 laptop, and I've come across a problem with running the filename parameter for PSQL. When attempting to run a SQL file with the line psql.exe -U <user> -f src/sql/test.sql the terminal hangs until I use Ctrl+C to exit the command. I can run psql -U <user> and then copy & paste the SQL file text into the terminal to get the results I want, but I don't get why this issue is happening in the first place.
I've checked my PATH environment variables and I do have both the /bin and /lib paths in there. I have also tested changing -f with the < operator, which didn't change anything. Running PSQL on Windows 8.1 isn't an issue, it's just this particular command.

Using the inline '--command' argument with psql doesn't produce logs in .psql_history

When I log into my PostgreSQL server manually on Ubuntu and execute a command, I can then find it logged in /root/.psql_history.
However when I try to run a command in a bash script via psql -c "*query goes here*", the command returns data but is not logged in .psql_history.
Has anyone encountered this before?
Command line retrieval and editing, as well as the history file, are functions of the “readline” library that is linked to psql.
Readline support is only active in interactive sessions, so there is also no history written if you invoke psql with the -c or -f options.

Error saving psql history file

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

PostgreSQL - read an SQL file into a PostgreSQL database from the commandline

I use Ruby to generate a bunch of SQL commands, and store this into a file.
I then login to my PostgreSQL database. Then I do something like:
\i /tmp/bla.sql
And this populates my database.
This all works fine as it is, no problem here.
I dislike the manual part where I have to use \i, though (because I need this to work in a cron job eventually, and I think commands like \i are only available when you are directly in the interactive psql prompt).
So my question now is:
Is it possible to use a psql command from the command line that directly will start to read in an external file?
You can directly use the psql command as shown below.
Works for me with Ubuntu and Mint. On Windows it should be quite the same...
psql -U user -d database -f filepath
Example:
psql -U postgres -d testdb -f /home/you/file.sql
For more information take a lock at the official documentation: http://www.postgresql.org/docs/current/static/app-psql.html
When you try to execute an sql file using cron, you will also need to set the environment - database name, password etc. This is a short shell script snippet that does it all
source /var/lib/pgsql/scripts/.pgenv
echo $PATH
psql << AAA
select current_date;
select sp_pg_myprocedure(current_date);
AAA
In .pgenv, you set the values such as
export PGPORT=<yourport>
export PGHOST=<yourhost>
export PGDATA=<yourdatadir>
Also have a .pgpass file so that the password is supplied.
http://www.postgresql.org/docs/current/static/libpq-pgpass.html
Replace the part where SELECT is being done with whatever you want to do, or do it as #Kuchi has shown.

How to correctly provide password to PostgreSQL when connecting remotely in Windows?

DB: PostgreSQL 9.0
Client: Windows 7
Server Windows 2008, 64bit
I'm trying to connect remotely to a PostgreSQL instance for purposes of performing a pg_dump to my local machine.
Everything works from my client machine, except that I need to provide a password at the password prompt, and I'd ultimately like to batch this with a script.
I've followed the instructions here:
http://www.postgresql.org/docs/current/static/libpq-pgpass.html
But it's not working.
To recap, I've created a file on the server: C:/Users/postgres/AppData/postgresql/pgpass.conf, where PostgreSQL is the db user.
The file has one line with the following data:
\*:5432:\*postgres:[mypassword]
I've also tried replacing each * with [localhost|myip] and [mydatabasename] respectively.
From my client machine, I connect using:
pg_dump -h <myip> -U postgres -w [mydbname] > [mylocaldumpfile]
I'm presuming that I need to provide the -w switch in order to ignore password prompt, at which point it should look in the AppData directory on the server.
It just comes back with:
connection to database failed: fe_sendauth: no password supplied.
As a hack workaround, if there was a way I could tell the Windows batch file on my client machine to inject the password at the PostgreSQL prompt, that would work as well.
It works for me:
Use command line
cd %appdata%
mkdir postgresql
cd postgresql
notepad pgpass.conf
inside pgpass.conf paste your connection string (*:5432:*postgres:[mypassword]) and save the file.
To connect to postgres use:
psql/pg_dump -U <username> -h <host> -w <other params you want to use>
I have solved similar problem (only in Linux) to use ip address in pgpass and psql.
.pgpass
127.0.0.1:5432:db:dbuser:123
psql params
psql -d db -U dbuser -h 127.0.0.1 -w
pg_hba conf with default settings:
# IPv4 local connections:
84 host all all 127.0.0.1/32 md5
Create pgpass.conf file
Windows > Start > Run
type %APPDATA%
press OK
Create a folder: postgresql
Create a file : pgpass.conf (under postgresql folder)
Open pgpass.conf file
Now you should have below file ready, open it via below (making sure it exists):
Windows > Start > Run
type %APPDATA%\postgresql\pgpass.conf
press OK
Paste pgpass.conf file contents
Paste the below
# serverDomainOrIP:PORT:databaseName:userName:password
# 127.0.0.1:5432:myDbName:postgres:myPassword
# *:5432:*:*:myPassword
You can do one of the below:
- Remove # for the 3rd line, and give your password in the place of "myPassword"
OR
- Remove # for the 2nd line, and give ip (or, yourDomain.com), dbname, username & password
Hope that helps.
I've had a similar problem which I didn't manage to resolve - I couldn't get the script to recognise the pgpass.conf file. I however used a work-around of setting the PGPASSWORD environment variable in the batch file I was using (PostgreSQL 9.6).
Inside a batch file:
SET PGPASSWORD=<<password>>
pg_dump.exe -h <<host>> -p <<port>> -U <<user>> -Fc -b -v -f <<output file path>> <<database>>
I have gotten it to work with the following:
pgpass.conf:
127.0.0.1:5432:*:username:password
However, I have it stored here:
C:\Users\<user>\AppData\Roaming\postgresql
For some reason, on a previous iteration of Postgres, the database had generated the pgpass file and stored it there. I was running into the same issue you were having, moved it to that directory and it worked. I'm not sure why though.
Then, all you'll need to do is:
pg_dump -h myip mydb > mylocaldumpfile
...ensuring that myip and the ip in pgpass.conf are identical. If they are not, it will prompt you for a password.
You could use pgAdmin III to store the password (in the server's properties).
That operation automatically creates a correct pgpass.conf file. You can then schedule a task to run a simple batch file that would read:
"C:\path\to\pg_dump.exe" -U <user> -w <database> > C:\path\to\database.backup
Make sure you are logged in as the user corresponding with the folder where the pgpass.conf file lives.
If you are using UTF-8 encoding, please ensure that you are using without BOM mode.
Otherwise leave the first line as a comment:
# This line may contain hidden BOM bytes
localhost:5432:database:username:password
Also you don't need to escape asterisks \*, just put * to enable wildcard matching.