Trying to restore database backup made with heroku pgbackups -tool.
I download backup by exposing url:
$ heroku pgbackups:url 'backup-name'
Created db with:
$ createdb 'dbname' -U postgres
And tried to restore from *.dump file:
$ psql -U postgres -d 'dbname' -f *.dump
I end up with following kind of syntax errors:
ERROR: syntax error at or near "PGDMP"
...
ERROR: invalid byte sequence for encoding "UTF8": 0x9d
HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding"
Ok, so this has something to do with encoding - but how I solve it?
Both config/application.rb and my postgres server has encoding set to UTF-8. database.yml has sqlite configured to it(haven't touched on production config). Gemfile has simply:
gem 'pg'
I found the answer directly from the manual:
$ curl -o latest.dump `heroku pgbackups:url`
$ pg_restore --verbose --clean --no-acl --no-owner -h myhost -U myuser -d mydb latest.dump
Related
I am trying to open a random .sql file off the internet using the following command:
psql -h localhost -d database_name -U postgres < file_name.sql
But when I run this command I just get errors like the following:
invalid command 's
invalid command 's
invalid command 'll
invalid command 'Moving
invalid command 's
invalid command "frequently
It just continuously prints out these invalid command error messages. I thought it might be an encoding problem but I confirmed the file is UTF-8 encoded.
Any suggestions on how I can open this file
To expand and clarify on a_horse_with_no_name's comment - the psql command you are running should be run directly in your shell, not inside pgadmin4.
youruser#yourmachine:~$ psql -h localhost -d database_name -U postgres < file_name.sql
That command should load the contents of file_name.sql in to database_name. Once it's complete, you can use pgadmin4 as normal to interact with the database.
One possibility is that the file contains tabulator keys, which are expanded if you read redirect standard input to the SQL script.
Try using the -f option:
psql -h localhost -d database_name -U postgres -f file_name.sql
Apparently the .sql file was generated through a MySQL dump. I thought it would not matter whether I used PostgreSQL or MySQL but it did. Once I installed MySQL my problem got resolved and I now have a Database ready :)
I would like to execute a psql query through ssh directly from local machine.
In remote machine, the command works fine:
psql -U USER -d DATABASE -c "select A,B,C from TABLE where A='string1';"
Now, when using:
ssh user#host "psql -U USER -d DATABASE -c "select A,B,C from TABLE where A='string1';""
I get errors such as:
psql: warning: extra command-line argument "A,B,C" ignored
psql: warning: extra command-line argument "from" ignored
psql: warning: extra command-line argument "TABLE" ignored
psql: warning: extra command-line argument "where" ignored
psql: warning: extra command-line argument "A=string1" ignored
I understand that the problem is due to the quotes as the following command in the local machine works fine:
ssh user#host "psql -U USER -d DATABASE -c '\l'"
How do I solve this?
Taking cues from this answer, the following command works:
ssh user#host 'psql -U USER -d DATABASE -c "select A,B,C from TABLE where A='"'"'string1'"'"';'
I recently moved my Ruby on Rails 4 app from Heroku to Linode. Everything has been setup correctly, but I need to populate my database with a file, lets call it movies.sql
I am not very familiar with postgresql command and VPS, so having trouble getting this done. I uploaded it to Dropbox since I saw many SO posts that you can use S3/Dropbox.
I saw different commands like this (unsure how to go about it in my situation):
psql -U postgres -d testdb -f /home/you/file.sql
psql -f file.sql dbname
psql -U username -d myDataBase -a -f myInsertFile
So which is the correct one in my situation and how to run when I SSH in Linode? Thanks
You'll need to get the file onto your server or you'll need to use a different command from your terminal.
If you have the file locally, you can restore without sshing in using the psql command:
psql -h <user#ip_address_of_server> -U <database_username> -d <name_of_the_database> -f local/path/to/your/file.sql
Otherwise, the command is:
psql -U <database_username> -d <name_of_the_database> < remote/path/to/your/file.sql
-U sets the db username, -h sets the host, -d sets the name of the database, and -f tells the command you're restoring from a file.
I have a data base named "mig". it has 10 tables. now i want to create a same database in another system so I am using mysqldump command but it shows error.
I entered command as follows :
mysqldump -u root -p root mig >file.sql;
This is the error i got :
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'mysql
dump -u root -p root mig >file.sql' at line 1
I am getting the same error when I use ,
mysqldump -u root -proot mig >file.sql;
How can i fix this ?
Simply try-
mysqldump -u root mig> file.sql
Edit
mysqldump is not a MySQL command, it is a command line utility. You must call it from your shell command line. I hope you are not calling this from MySQL prompt.
When providing password on the command line you should leave no space after -p.
It should look smth like:
mysqldump -u root -proot mig >file.sql;
You can use some tools like MySQL Workbench or SQLyog to import the dump file.
Free version: https://code.google.com/p/sqlyog/wiki/Downloads
When you execute mysqldump from command line, you must have mysql_home/bin directory in your classpath variable or command-line must be pointing to it.
try using
mysqldump -u root -proot mig >(abs_path)/file.sql;
This works for me on my local. Open Terminal and execute the following code (Make sure your are NOT on the MySQL prompt):
mysqldump -uroot -p mig > file.sql
It will ask you to input the password on the next line, for security the password won't be shown.
If you get Access Denied, means the mysql credentials are wrong (or the user you use don't have the right permissions to generate a dump), so make sure you have a valid username and password. I hope it helps.
mysqldump will not run from mysql cli, you will have to run it from windows command prompt:
mysqldump -u username -p database_name > output_file_name.sql;
If you are getting error on running above command 'mysqldump is not recognized as an internal or external command' then navigate to < MySQL Installation Directory/bin/ > and then run the command.
i have the same problem, my situation was i connect from client in local computer to server in SQL instance of Google. Since i read Sahil Mittal said this is comman utilty, i just put in terminal the same command adding -h parameter.
mysqldump -h ip.del.host -u root -p database_name > database_desired_name.sql
I have downloaded the database from an existing Heroku app onto my local machine. The data I downloaded is in a single file; let's call it herokuapp.db. I also have Postgres installed and I can successfully create a new Postgres database and have my Rails app reference that. What I want to do is move the data I downloaded from Heroku into this database or create a new Postgres database using the downloaded data.
Use pg_restore --verbose --clean --no-acl --no-owner -d [database_name] [herokuapp.db]
see https://devcenter.heroku.com/articles/export-from-heroku-postgres for more information
I just solved the same problem with a similar technique that Will suggested (thanks Will!).
$ heroku pgbackups:capture
$ curl -o latest.dump `heroku pgbackups:url`
$ pg_restore --verbose --clean --no-acl --no-owner -d [database_name] latest.dump
The database_name can be found in the development section of the database.yml file.
EDIT
I recently performed this task again with Postgres.app and the last command above did not work. I was getting this error message:
pg_restore: connecting to database for restore
pg_restore: [archiver (db)] connection to database "[database_name]" failed: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
pg_restore: *** aborted because of error
Here is the updated command that worked:
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U $USER -d [database_name] latest.dump