I'm trying to restore my dump file.
I am working on a virtual machine with windows. When I try to import the file into my new database I get the following error:
no such file or directory.
I tried with different ways to fix the problem, but I still can not import my dump file. I hope somebody can give me some advice how can I fix it.
When I try to import I used the \i meta command and the file path.
examples:
NewDB# \i 'c:/Program Files/PostgreSQL/9.5/scripts/file.sql'
I read some advices and some of them mentioned move the file to different directories, I tried also this, but I still have the problem.
pythonanywhere bash restore postgresql from dump:
find out name of database and name of user:
postgres=# \c
psql (12.7 (Ubuntu 12.7-1.pgdg20.04+1), server 12.2 (Ubuntu 12.2-2.pgdg16.04+1))
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
You are now connected to database "postgres" as user "super".
get address and port on Databases Postgres tab e.g.:
magula10-2617.postgres.pythonanywhere-services.com
12617
restore database named "postgres" (user "super") from dump (will prompt for postgresql password):
$ psql -h magula10-2617.postgres.pythonanywhere-services.com -p 12617 -U super -W postgres < 2022_4_4_localhost_dump
Related
I was trying to create a new database (analyses_db) on a remote server from a sql file by the command:
pg_restore -d analyses_db byoryn_resource.sql
I received the error message
pg_restore: [archiver] input file appears to be a text format dump. Please use psql.`
When I tried to follow the instruction: (from https://stackoverflow.com/a/40632316/15721796)
To reload such a script into a (freshly created) database named newdb:
$ psql -d newdb -f db.sql
I received:
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"?
I have no idea how to solve this, as a rookie. The SQL file should be alright as it is provided.
After the connection error being solved, I tried the command
sudo -u postgres psql db_name < 'file_path'
(from https://stackoverflow.com/a/26610212/15721796)
which works just fine.
Hope this can help someone who has the same problem.
Also some useful doc here https://www.postgresql.org/docs/9.1/backup-dump.html
I am using Postgres via a vagrant instance running ubuntu-xenial-16.04-cloudimg box and I have an sql dump from another developer.
By the way, I tried using PGAdmin IV from my Win 10 host machine after I had connected to the Postgres server on the virtualbox (ubuntu) but it takes forever and not running.
How can I import this to the Postgres running on virtualbox instance?
So given an sql dump file as dump.sql.
Run vagrant ssh on an ssh client like git bash(for windows)
Put the dump file in the directory containing the vagrantfile on the host machine. As it syncs by default with the guest machine or run vagrant rsync, just to make sure.
Navigate to the vagrant directory on the host machine (eg cd ../../ for an ubuntu guest on a window host)
Run psql -h hostname -U test -d databasename -f dump.sql.
Depending on the format of the dump (normal or custom), you can use psql or pg_restore.
Check the --format option in the documentation for pg_dump
Following the simple steps below solves my problem:
After vagrant up, vagrant ssh to log into the os
Type psql command
Then create database your_db_name to create the empty db
Make sure the dump sql file is in the folder containing the vagrantfile ( cd vagrant) or a sub folder within
Write this command to import the dump file into the newly create db
your_db_name -f /path/to/the/dump.sql
I hope the steps will help you too.
Due to version incompatibilities of my postgres database on heroku (9.1) and my local installation (8.4) I need a plain text sql database dump file so I can put a copy of my production data on my local testing environment.
It seems on heroku I can't make a dump using pg_dump but can instead only do this:
$ heroku pgbackups:capture
$ curl -o my_dump_file.dump `heroku pgbackups:url`
...and this gives me the "custom database dump format" and not "plain text format" so I am not able to do this:
$ psql -d my_local_database -f my_dump_file.sql
You could just make your own pg_dump directly from your Heroku database.
First, get your postgres string using heroku config:get DATABASE_URL.
Look for the Heroku Postgres url (example: HEROKU_POSTGRESQL_RED_URL: postgres://user3123:passkja83kd8#ec2-117-21-174-214.compute-1.amazonaws.com:6212/db982398), which format is postgres://<username>:<password>#<host_name>:<port>/<dbname>.
Next, run this on your command line:
pg_dump --host=<host_name> --port=<port> --username=<username> --password --dbname=<dbname> > output.sql
The terminal will ask for your password then run it and dump it into output.sql.
Then import it:
psql -d my_local_database -f output.sql
Assuming you have a DATABASE_URL configured in your environment, there is a far simpler method:
heroku run 'pg_dump $DATABASE_URL' > my_database.sql
This will run pg_dump in your container and pipe the contents to a local file, my_database.sql. The single quotes are important. If you use double quotes (or no quotes at all), DATABASE_URL will be evaluated locally rather than in your container.
If your whole purpose is to load the contents into a local database anyways, you might as well pipe it straight there:
createdb myapp_devel # start with an empty database
heroku run 'pg_dump -xO $DATABASE_URL' | psql myapp_devel
The addition of -xO avoids dumping GRANT, REVOKE, and ALTER OWNER statements, which probably don't apply to your local database server. If any of your COPY commands fail with the error ERROR: literal carriage return found in data (mine did), see this answer.
It's quite possible this didn't work two and a half years ago when this question was originally asked, but for those looking for a way to easily get a dump of your Heroku Postgres database, this appears to be the simplest possible way to do this today.
Heroku's PGBackups actually uses pg_dump behind the scenes, and the "custom format" is actually pg_dump's custom format (-Fc parameter), not Heroku's own custom format.
This means you can use pg_restore, which is part of Postgres, to restore your Heroku backup into another database directly:
pg_restore -d mydatabase my_dump_file.dump
In addition, if you call pg_restore without specifying a database to restore to, it'll print SQL statements to standard out, so you can turn your Heroku backup into a SQL file that way:
pg_restore my_dump_file.dump > sql_statements.sql
UPDATE: on more recent versions of postgres, the following command is required (thanks to comment from PatKilg)
pg_restore latest.dump -f - > sql_statements.sql
for people like me that stumble into this problem in 2020:
heroku pg:backups:capture -a app-name
heroku pg:backups:download -a app-name
the tool will actually tell what command to use after the capture. To get SQL from your latest.dump file:
pg_restore -f sqldump.sql latest.dump
and that's it.
pg_dump accepts a connection string so you don't need to deconstruct it manually like mentioned here: https://stackoverflow.com/a/22896985/3163631.
Let's say your connection string looks like this (I randomized the username and pass and added fillers for the remaining. The "shape" of the connection string is correct):
postgres://Nb6n8BTA4rPK5m:DzEPtwZUkJfgbMSdYFUbqupvJeEekihiJNzqGXa3wN2pmYRGcLQ8Sa69ujGn2RSkb#ec2-00-000-000-000.compute-1.amazonaws.com:5432/j4aaaaaaaaaam1
Even though it is in the postgres://<username>:<password>#<host_name>:<port>/<dbname> format, you can use it directly like so:
pg_dump postgres://Nb6n8BTA4rPK5m:DzEPtwZUkJfgbMSdYFUbqupvJeEekihiJNzqGXa3wN2pmYRGcLQ8Sa69ujGn2RSkb#ec2-00-000-000-000.compute-1.amazonaws.com:5432/j4aaaaaaaaaam1 > output.sql
Maybe this was not possible with pg_dump at the time Alex(https://stackoverflow.com/users/3457661/alex) answered in 2014.
Here's what worked for me:
heroku pg:backups:capture
heroku pg:backups:download
pg_restore latest.dump -f latest.sql
psql -f 'latest.sql' -d '<DEV_DB_NAME>'
Explanation:
First we create a snapshot of the database on Heroku
Then we download the snapshot as 'latest.dump' (the name can be changed using -o '<name>.dump')
Convert the binary dump into plain SQL, which can be imported without raising "pg_dump: error: aborting because of server version mismatch"
Import the file into the local database
Of course, if your running version of postgresql is compatible with Heroku's, heroku pg:pull DATABASE_URL <DEV_DB_NAME> is simpler to type and remember.
Heroku pg:backups:capture
Heroku pg:backups:download
Taken from https://devcenter.heroku.com/articles/heroku-postgres-import-export.
Now you have a binary file. To obtain the file in plain text format, the following worked for me. Note: You will need to install PostgreSQL.
pg_restore latest.dump > latest.sql
You could just download the Heroku dump file and convert it into plain text format.
In newer versions, directly redirecting the output of pg_restore to an SQL file won't work. Doing so will produce an error:
pg_restore my_dump_file.dump > my_dump_file.sql
pg_restore: error: one of -d/--dbname and -f/--file must be specified
Instead, to output the result in plain text format, -f should be used:
pg_restore my_dump_file.dump -f my_dump_file.sql
This will convert the heroku "custom database dump format" to "plain text format".
Then import this file:
psql -d my_local_database -f my_dump_file.sql
ok, following these instructions I've runned:
$ PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump
in the same folder where the database is (app_db.sql) but I keep getting:
pg_dump: [archiver (db)] connection to database "mydb" failed: FATAL: database "mydb" does not exist
Why is this happening and what can I do solve it?
Thanks
You have critically misunderstood how PostgreSQL works.
app_db.sql is not a database, it is a database dump. A sequence of text commands that describe the data in the database that can be replayed to create the database.
It isn't like a Microsoft Access .dbx file or a SQLite3 database file, a database stored in a single file that can be manipulated directly. The only way to work with a PostgreSQL database is via a client/server connection to the PostgreSQL server, which stores the actual database contents in a system dependent location like /var/lib/pgsql which you never need to manipulate directly.
To use the database dump, you must restore it into a new empty database using the psql command, eg:
$ createdb mydb
$ psql -f app_db.sql mydb
This will probably fail with permissions errors if you try to run it exactly as written above. You will need to create yourself a user, give that user ownership of mydb, and possibly edit pg_hba.conf to allow yourself to authenticate depending on your system settings.
A more realistic example for a user with unix login name bob might be:
$ sudo -u postgres createuser bob
$ sudo -u postgres createdb -O bob mydb
$ psql -f dpp_db.sql -1 -v ON_ERROR_STOP=1 mydb
I strongly recommend that you read the PostgreSQL tutorial and the user manual.
I have a .sql file that was created by postgresql a while back. I now want to import this file onto a windows machine running postgresql.
How do I do this. The file is about 1.5gb.
You should use psql command line tool:
psql -h hostname -p port_number -U username -f your_file.sql databasename
click on the SQL Shell and log into the database and use import
Server [localhost]:
Database [postgres]:
Port [5432]:
Username [postgres]:
Password for user postgres:
psql (9.2.4)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
postgres=# \i c:/data/data01.sql
start you psql command tool, it will give you dialog like the following
Server [localhost]:
Database [postgres]:
Port [5432]:yourport
Username [postgres]:
Password for user postgres:**********
then connect to your database
postgres=# \c yourdatabase;
then import the file
yourdatabase=# \i c:/path/path/data/data01.sql
note the / for directory separator & no spaces in file path
This also works for me:
psql dbname username < file.sql
command prompt
open your cmd window and type the following (make sure the path of postgres is correct)
."C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -h 127.0.0.1 -p 5432 -U postgres -d dbname <./query.sql
psql -U <dbusername>
if the prompt makes you enter password, do that.
\c <yourdatabasename>
\i 'thepathusing/delimiter.sql'
Two points you need to watch out that
Use / as writing path of the file instead of \.
Use single quote
symbol ' instead of ".
If you're doing it with a URI connection string make sure the arguments are before the URI, Powershell examples:
Works on windows:
.\psql -f TestFile.sql $connString
.\psql -c 'SELECT Version();' $connString
Won't work on windows (URI connection before arguments):
.\psql $connString -c 'SELECT Version();'