"[Errno 2] No such file or directory" when trying to backup a database in pgAdmin4 - pgadmin-4

I have finally managed to get around managing to manually set "PostgreSQL Binary Path" in preferences (why should the user have to do this when it wasn't necessary in pgAdmin3?).
Though to be honest, I have no idea what is meant with the second path field called "EDB Advanced Server Binary Path". But I get no more error message concerning that so I assume it is o.k. to leave that empty.
Now I am stuck on the next step.
When trying to backup the database I have to enter a filename. No matter what I do here, I keep getting this error:
[Errno 2] No such file or directory
Well, it can not be that I have to create a file before it is created by pgAdmin, can it?
What could I be doing wrong?
Alex
update: answering to #n33rma here are the print screens:

I solved the problem through the command line.
If we use a previous format, it has the same response as you had:
$ pg_restore -d <DBNAME> --username=postgres g=<FILENAME>
pg_restore: [archiver] could not open input file "g=<FILENAME>": No such file or directory
If we change the last param, it works properly:
$ pg_restore -d <DBNAME> --username=postgres <FILENAME>
I suppose the GUI-command changed in v4, but the console pg_restore is not.

I just updated to pgAdmin v4.1 and this solved the issue :-)
Thanks all for helping!

You need to give absolute path with the file name to sql file like below.
Windows
C:/test/test.sql
Linux
/u01/test.sql

Enter only a filename, no path, because pgadmin puts the backup in its own directory, specifically, in ~/.pgadmin/storage/(user).

I suspect that path for "PostgreSQL Binary Path" is not set properly in pgAdmin4.
pgAdmin4 is Web application and pgAdmin3 is Desktop application, So pgAdmin4 needs to know path of utilities like pg_dump & pg_restore binaries so that it can execute them.
In pgAdmin4 Goto: File > Preferences > Paths > Binary paths > PostgreSQL Binary Path
If you are using Windows than provide path like,
C:\Program Files\PostgreSQL\9.6\bin
If you are using Linux than provide path like,
/opt/PostgreSQL/9.5/bin
user#mint:/opt/PostgreSQL/9.5/bin$ lsh pg_du* pg_res*
-rwxr-xr-x 1 xxx xxx 150K Sep 2 2015 pg_restore
-rwxr-xr-x 1 xxx xxx 49K Sep 2 2015 pg_resetxlog
-rwxr-xr-x 1 xxx xxx 83K Sep 2 2015 pg_dumpall
-rwxr-xr-x 1 xxx xxx 364K Sep 2 2015 pg_dump

On MacOS 10.11 and using postgres.app I resolved it this way:

Related

postgresql error connecting after moving data directory

EDIT-2
I found out that the database doesn't even start after making the file location change.
This is with the default file location:
$pg_isready
/var/run/postgresql:5432 - accepting connections
$pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
9.5 main 5432 online postgres /var/lib/postgresql/9.5/main /var/log/postgresql/postgresql-9.5-main.log
pg_lsclusters output is green.
After the file location has changed on postgresql.conf:
$pg_isready
/var/run/postgresql:5432 - no response
$pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
9.5 main 5432 down root /mnt/Data/postgresdb/postgresql/9.5/main /var/log/postgresql/postgresql-9.5-main.log
Here the output is red.
Following this post here, I tried to start the cluster manually:
$pg_ctlcluster 9.5 main start
Warning: the cluster will not be running as a systemd service. Consider using systemctl:
sudo systemctl start postgresql#9.5-main
Error: You must run this program as the cluster owner (root) or root
I tried the same command with sudo:
Error: Config owner (postgres:124) and data owner (root:0) do not match, and config owner is not root
Which again makes me think the problem might lie with permissions of the directory. The directory is owned by root whose ownership I am unable to change.
EDIT-1
I've been working on this and I'd like to distill this post further to give more specifics. This is my current situation:
I installed postgres: sudo apt-get install postgresql and postgresql-contrib
I used sudo -U postgres psql to get into the postgres shell (I'm not sure if this is what I need to do)
show data_directory returns: /var/lib/postgresql/9.5/main
The data directory is located in Ubuntu ext4 formatted hard drive. I also have a 1 TB NTFS formatted hard disk mounted on /mnt/Data (which is mounted automatically on boot). What I tried:
Stop the postgres service: sudo systemctl stop postgresql
Create a new directory /mnt/Data/postgresdb and copy contents of the previous main to this which gives me a full path of /mnt/Data/postgresdb/postgresql/9.5/main using: sudo rsync -av /var/lib/postgresql/ /mnt/Data/postgresdb/postgresql/
Edit /etc/postgresql/9.5/main/postgresql.conf to change data_directory from the path mentioned above to /mnt/Data/postgresdb/postgresql/9.5/main
Start the postgres service: sudo systemctl start postgresl
Run sudo -U postgres psql but get the error that was mentioned in the original post.
These are the permissions on the respective main directories:
ls -l /var/lib/postgresql/9.5/
total 4.0K drwx------ 19 postgres postgres 4.0K Jan 16 12:40 main
ls -l /mnt/Data/postgresdb/postgresql/9.5/
total 4.0K drwxrwxrwx 1 root root 4.0K Jan 16 12:13 main
From the looks of it, the default directory is owned by "postgres" and the new directory is owned by root. However, when I try to change ownership to postgres: chown -R postgres main, it doesn't output any error, but the ownership doesn't change. I'm curious whether this is because this drive is NTFS formatted and is mounted.
Here is my /etc/fstab:
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda5 during installation
UUID=3f5a9875-89a3-4ce5-b778-9d7aaf148ed6 / ext4 errors=remount-ro 0 1
# swap was on /dev/sda6 during installation
UUID=85c3f4d4-e450-435b-8dd6-cf1b2cbd8fc2 none swap sw 0 0
/dev/disk/by-label/Data /mnt/Data auto nosuid,nodev,nofail,x-gvfs-show 0 0
Any ideas on how I can go about fixing this?
ORIGINAL POST
Recently, I installed Postgresql for storing some data for my research. The dataset came with instructions on how to setup the data on a Postgresql database (if interested, more info on that here and here). I installed Postgresql and set up a "role" and used the script that was provided for loading the database. It worked but I underestimated the size of the dataset and the script quit saying there was no more space.
I have two drives on my computer a 250G SSD drive with Windows and Ubuntu installed (125G each). And a 1TB HDD NTFS formatted where I store my data. So I thought moving the database to a folder on the other drive would be helpful. I purged all the data and the database to start afresh and followed the instructions here to move the database directory. However, after moving the directory, when I try to connect using psql I get the following error:
~ psql -U username -d postgres 14:48:33
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
How can I fix this? I am running 64-bit Ubuntu 16.04 with Postgresql-9.5. As mentioned earlier, I moved the DB directory a NTFS formatted filesystem (not sure if that cause any problems).
Thanks.
As mentioned in the comments the NTFS was the problem. I ended up resizing my bigger hard drive with 100GB formatted as ext4 and was able to launch postgres with the new data directory without any problems.

Issue running Postgresql 9.6 backup (pg_dump/pg_dumpall)

I have a postgreSQL 9.6 installation on Ubuntu 16.04, provided by the EnterpriseDB. I've been connecting to the local server and using it for a number of months. I figured I should backup the server at this point, so I tried using the pg_dump command as follows:
cd to postgres bin at /opt/PostgreSQL/9.6/bin
Run pg_dump via sudo pg_dumpall > /sata_ssd2/pgdump/0419.bkp
This gives an error: Error: You must install at least one postgresql-client-<version> package. I tried installing via: sudo apt-get install postgresql-client-9.6 but that package does not seem to exist.
I also tried to run pg_dump and pg_dumpall via pgAdmin4. It first made me fill in the binary paths for EDB Advanced Server Binary Path and PostgreSQL Binary Path; I put in opt/PostgreSQL/9.6/bin/, which is where the pg_dump executable is. When I tried to run the backups, I got the error: 'ascii' codec can't decode byte 0xe1 in position 1: ordinal not in range(128). I found an answer to this elsewhere that recommended changing from client_encoding = SQLascii to client_encoding = utf8 in postgresql.conf, but this did not help.
Does anyone have any advice on where to go from here?
In a unix/linux system the current directory . is not on the PATH and not searched for binaries.
If you want to run the command pg_basebackup in /opt/PostgreSQL/9.6/bin/, you run /opt/PostgreSQL/9.6/bin/pg_basebackup.
If you did want to run the command pg_basebackup that you knew was in the current directory you could write ./pg_basebackup instead. But just using the full path rather than cding unnecessarily is better.

postgresql where does the output of pg_dump go

I am trying to backup a db of postgresql and I want to use pg_dump command.
I tried :
psql -U postgres
postgres-# pg_dump test > backup.sql
But I don't know where the output file goes.
Any help will be appreciated
I'm late to this party, but I feel that none of the answers are really correct. Most seem to imply that pg_dump writes a file somewhere. It doesn't. You are sending the output to a file, and you told the shell where to write that file.
In your example pg_dump test > backup.sql, which uses the plain or SQL format, the pg_dump command does not store any file anywhere. It just sends the output to STDOUT, which is usually your screen, and it's done.
But in your command, you also told your shell (Terminal, Command prompt, whatever) to redirect STDOUT to a file. This has nothing to do with pg_dump but is a standard feature of shells like Bash or cmd.exe.
You used > to redirect STDOUT to a file instead of the screen. And you gave the file name: "backup.sql". Since you didn't specify any path, the file will be in your current directory. This is probably your home directory, unless you have done a cd ... into some other directory.
In the particular case of pg_dump, you could also have used an alternative to the > /path/to/some_file shell redirection, by using the -f some_file option:
-f file --file=file
Send output to the specified file. This parameter can be omitted for file based output formats,
in which case the standard output is used.
So your command could have been pg_dump test -f backup.sql, asking pg_dump to write directly to that file.
But in any case, you give the file name, and if you don't specify a path, the file is created in your current directory. If your prompt doesn't already display your current directory, you can have it shown with the pwd command on Unix, and cd in Windows.
Go to command prompt and directory postgresql\9.3\bin.
Example
.
..
c:\Program files\postgresql\9.3\bin> pg_dump -h localhost -p 5432 -U postgres test > D:\backup.sql
...
After above command enter User "postgres" password and check D:\ drive for backup.sql file
In my situation (PostgreSQL 9.1.21, Centos 6.7), the command
runuser -l postgres -c 'pg_dump my_database > my_database.sql'
saved the file here:
/var/lib/pgsql/my_database.sql
Not sure if that is true for other Linux dists, CentOS and/or pgl versions. According to the answer post by the asker of this question, this is true, but other users said the backup file was in the current directory (a situation different of most people reading this thread, for obvious reasons). Well, I hope this can help other users with the same problem.
P.s.: if that's not the path for your situation, you can try (in Linux) to find it using the below command (as stated by #Bohemian in the comments of this question), but this can take a while:
find / -name 'my_database.sql'
EDIT: I tried to run the analogous command in Ubuntu 12.04 (it works on Ubuntu 18.04):
sudo -u postgres pg_dump my_database > my_database.sql
And in this case the file was saved in the current directory where I ran the command! So both cases can happen in Linux, depending of the specific dist you are working
For Linux default dump path is:
/var/lib/postgresql/
If you are not specifying fully qualified paths, like:
pg_dump your_db_name > dbdump
then in Windows it stores dumps in current user's home directory. I.e.:
C:\Users\username
If you use linux (except centos)
sudo su - postgres
pg_dump your_db_name > your_db_name.sql
cd /var/lib/postgresql
ls -l
Here your'll see your_db_name.sql file
In pgadmin 4 for a Mac, assuming dump is successful you can click on "More Details" you will see a box that says "Running command:" in that box you will see /Applications/pgAdmin 4.app/Contents/SharedSupport/pg_dump --file "path/to/file" where path to file is the destination of storage.
After doing
psql -U postgres
Using the command
\! pg_dump -U postgres humaine > C:\Users\saivi\OneDrive\Desktop\humaine_backup1.sql
The output file would go where the path at the right is specified
In the server (Ubundu/Centos) the path of backup file will be
/var/lib/pgadmin/storage/
Below is the OS specification.
NAME="Ubuntu"
VERSION="20.04 LTS (Focal Fossa)"
I am using following command to take the backup of postgresql database.
pg_dump -U postgres -Fc <db_name> > /var/lib/postgresql/backup-20230123.dump
If storage file path has been provided explicitly, in that case, the database dump will be generated to that place only.
For windows, provide folder path where you want to download the dump.

fatal error when starting postgres on solaris

I'm trying to install postgres on a sun solaris sparc instance in my home directory.
Everything is fine except when I try to start postgres server, I get the following error
FATAL: "/home/reic/var/lib/pgsql/data" is not a valid data directory
DETAIL: File "/home/reic/var/lib/pgsql/data/PG_VERSION" is missing.
I used the command pg_ctl -l logfile start to start the server.
I have followed all the necessary steps for installation on sun solaris.
Any idea as to why is this happening ?
Solution:
All the necessary files are there in the PGDATA directory and I ran it as the same user which is trying to start the server. The problem is with the placement of env variables in the .bashrc instead of .bash_profile file. Apparently the customisations are not propagated to sub shells when the env variables are put in .bashrc.
My bad for not realising this! Thank you all....

Import SQL dump into PostgreSQL database

We are switching hosts and the old one provided a SQL dump of the PostgreSQL database of our site.
Now, I'm trying to set this up on a local WAMP server to test this.
The only problem is that I don't have an idea how to import this database in the PostgreSQL 9 that I have set up.
I tried pgAdmin III but I can't seem to find an 'import' function. So I just opened the SQL editor and pasted the contents of the dump there and executed it, it creates the tables but it keeps giving me errors when it tries to put the data in it.
ERROR: syntax error at or near "t"
LINE 474: t 2011-05-24 16:45:01.768633 2011-05-24 16:45:01.768633 view...
The lines:
COPY tb_abilities (active, creation, modtime, id, lang, title, description) FROM stdin;
t 2011-05-24 16:45:01.768633 2011-05-24 16:45:01.768633 view nl ...
I've also tried to do this with the command prompt but I can't find the command that I need.
If I do
psql mydatabase < C:/database/db-backup.sql;
I get the error
ERROR: syntax error at or near "psql"
LINE 1: psql mydatabase < C:/database/db-backu...
^
What's the best way to import the database?
psql databasename < data_base_dump
That's the command you are looking for.
Beware: databasename must be created before importing.
Have a look at the PostgreSQL Docs Chapter 23. Backup and Restore.
Here is the command you are looking for.
psql -h hostname -d databasename -U username -f file.sql
I believe that you want to run in psql:
\i C:/database/db-backup.sql
That worked for me:
sudo -u postgres psql db_name < 'file_path'
I'm not sure if this works for the OP's situation, but I found that running the following command in the interactive console was the most flexible solution for me:
\i 'path/to/file.sql'
Just make sure you're already connected to the correct database. This command executes all of the SQL commands in the specified file.
Works pretty well, in command line, all arguments are required, -W is for password
psql -h localhost -U user -W -d database_name -f path/to/file.sql
Just for funsies, if your dump is compressed you can do something like
gunzip -c filename.gz | psql dbname
As Jacob mentioned, the PostgreSQL docs describe all this quite well.
make sure the database you want to import to is created, then you can import the dump with
sudo -u postgres -i psql testdatabase < db-structure.sql
If you want to overwrite the whole database, first drop the database
# be sure you drop the right database !!!
#sudo -u postgres -i psql -c "drop database testdatabase;"
and then recreate it with
sudo -u postgres -i psql -c "create database testdatabase;"
Follow the steps:
Go to the psql shell
\c db_name
\i path_of_dump [eg:-C:/db_name.pgsql]
I tried many different solutions for restoring my postgres backup. I ran into permission denied problems on MacOS, no solutions seemed to work.
Here's how I got it to work:
Postgres comes with Pgadmin4. If you use macOS you can press CMD+SPACE and type pgadmin4 to run it. This will open up a browser tab in chrome.
If you run into errors getting pgadmin4 to work, try killall pgAdmin4 in your terminal, then try again.
Steps to getting pgadmin4 + backup/restore
1. Create the backup
Do this by rightclicking the database -> "backup"
2. Give the file a name.
Like test12345. Click backup. This creates a binary file dump, it's not in a .sql format
3. See where it downloaded
There should be a popup at the bottomright of your screen. Click the "more details" page to see where your backup downloaded to
4. Find the location of downloaded file
In this case, it's /users/vincenttang
5. Restore the backup from pgadmin
Assuming you did steps 1 to 4 correctly, you'll have a restore binary file. There might come a time your coworker wants to use your restore file on their local machine. Have said person go to pgadmin and restore
Do this by rightclicking the database -> "restore"
6. Select file finder
Make sure to select the file location manually, DO NOT drag and drop a file onto the uploader fields in pgadmin. Because you will run into error permissions. Instead, find the file you just created:
7. Find said file
You might have to change the filter at bottomright to "All files". Find the file thereafter, from step 4. Now hit the bottomright "Select" button to confirm
8. Restore said file
You'll see this page again, with the location of the file selected. Go ahead and restore it
9. Success
If all is good, the bottom right should popup an indicator showing a successful restore. You can navigate over to your tables to see if the data has been restored propery on each table.
10. If it wasn't successful:
Should step 9 fail, try deleting your old public schema on your database. Go to "Query Tool"
Execute this code block:
DROP SCHEMA public CASCADE; CREATE SCHEMA public;
Now try steps 5 to 9 again, it should work out
Summary
This is how I had to backup/restore my backup on Postgres, when I had error permission issues and could not log in as a superuser. Or set credentials for read/write using chmod for folders. This workflow works for a binary file dump default of "Custom" from pgadmin. I assume .sql is the same way, but I have not yet tested that
I use:
cat /home/path/to/dump/file | psql -h localhost -U <user_name> -d <db_name>
Hope this will help someone.
If you are using a file with .dump extension use:
pg_restore -h hostname -d dbname -U username filename.dump
I noticed that many examples are overcomplicated for localhost where just postgres user without password exist in many cases:
psql -d db_name -f dump.sql
You can do it in pgadmin3. Drop the schema(s) that your dump contains. Then right-click on the database and choose Restore. Then you can browse for the dump file.
I used this
psql -d dbName -U username -f /home/sample.sql
Postgresql12
from sql file:
pg_restore -d database < file.sql
from custom format file:
pg_restore -Fc database < file.dump
I had more than 100MB data, therefore I could not restore database using Pgadmin4.
I used simply postgres client, and write below command.
postgres#khan:/$ pg_restore -d database_name /home/khan/Downloads/dump.sql
It worked fine and took few seconds.You can see below link for more information.
https://www.postgresql.org/docs/8.1/app-pgrestore.html

Resources