32

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?

Don
  • 3,876
  • 10
  • 47
  • 76
  • 8
    In `psql` you could to use `\!` meta-command to execute shell command. For example `\! pwd` to show working directory. – Abelisto Jul 13 '17 at 22:24
  • 1
    @Abelisto that works. I feel silly. I usually work via command line psql in a linux console/VM and neglected that I was on a Windows machine at first so I actually needed to use `\! echo %cd%` and the default dir is unsurprisingly 'C:\Program Files\PostgreSQL\9.4\bin'. Thanks! – Don Jul 13 '17 at 23:10
  • 1
    `\! echo %cd%` can be simplified to `\! cd` –  Nov 28 '17 at 21:00

2 Answers2

30

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.

  • 2
    To clarify: `\cd ...` is a PSQL command (type `\?` for a list, so `\cd /home/victoria` (no exclamation point), *but* `\!pwd` to print that directory path, or `\! ls -l` to list the contents. – Victoria Stuart Jun 08 '18 at 20:35
  • Well that didn't work... I get `postgres-# \! pwd 'pwd' is not recognized as an internal or external command, operable program or batch file.` – user32882 Dec 10 '18 at 09:28
  • 4
    Are you on Windows @user32882? The reason I ask, `cd` is the Windows command-line equivalent of the -nix shell command `pwd`. To expand a little, the Postgres `\!` command is described as a meta-command because it tells the machine "for this line only, I want to you to forget that we're in a database and we'll speak our regular language instead." So `\!` is followed by regular shell-scripting language appropriate to whatever OS you're on. `\! cd` should print your working directory to the screen on a Windows machine. – amunnelly Jan 05 '19 at 19:44
8

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.

Rich Lysakowski PhD
  • 2,702
  • 31
  • 44
  • 1
    `postgres is now becoming a very popular DBMS on Windows 10`? There are signs that Windows encourages using it on Linux as an integrated part of Windows instead, in the new times of WSL2 and Docker Desktop. At least, I could not get the plpython3u extension to work on Windows, see [PostgreSQL 13 + Python 3.7.9 + plpython3u: 'psql: server closed the connection unexepectedly.' + 'The application has lost the database connection.'](https://stackoverflow.com/questions/67852675/postgresql-13-python-3-7-9-plpython3u-psql-server-closed-the-connection-u). – questionto42 Sep 02 '21 at 06:57