import sql dump into postgres [duplicate] - postgresql

This question already has answers here:
Import SQL dump into postgresql [closed]
(3 answers)
Closed 10 years ago.
when i run this command on ubuntu terminal:
psql -h localhost -U arwinder --password -f sh_cake_db_14_02_13.sql -d new_db
where "arwinder" is user name and "sh_cake_db_14_02_13.sql" is sql dump and "new_db" is database.
then while importing it shows following errors and in postgresql database I don't have all the tables. Tese are the errors:
invalid command \N
invalid command \
invalid command \
ERROR: syntax error at or near "27"
LINE 1: 27 Balaji Angan

There is a convert tool https://github.com/lanyrd/mysql-postgresql-converter ,or you can refer to postgressql's wiki http://wiki.postgresql.org/wiki/Converting_from_other_Databases_to_PostgreSQL , see MySQL section and check your data types.

Related

Copying local postgresql to AWS RDS

I have a Postgesql database that I want to copy/replicate from my local machine to an AWS RDS instance.
I've created a dump as follows:
pg_dump -v -U postgres mydb > mydb.dump
I then tried to copy to my RDS as follows:
psql -f mydb.dump -h XXXXXXXXXXXX.us-east-1.rds.amazonaws.com -p 5432 -U postgres -d mydb
However, I get the following error:
psql:mydb.dump:1: error: invalid command \
psql:mydb.dump:6: ERROR: syntax error at or near "ÿ_"
LINE 1: ÿ_-"\o""edrp\nou"tnsctme e
^
I then tried to rule out any issues with RDS by copying the archive to another local database as follows:
pg_restore -U postgres -d mydbcopy mydb.dump
I received an error:
pg_restore: error: input file does not appear to be a valid archive
I also tried the preceding as follows:
psql -f my.dump -U postgres -d mydbcopy
And got the same error as before:
psql:mydb.dump:1: error: invalid command \
psql:mydb.dump:6: ERROR: syntax error at or near "ÿ_"
LINE 1: ÿ_-"\o""edrp\nou"tnsctme e
^
I am using Windows Powershell and Postgresql 13.2.
Am I missing something ?
It seems to be an issue with PSQL tools in view that I'm getting the error even when trying to copy locally.
I managed to solve this problem by using
pg_dump -f dump.sql --inserts mydb
It turns out using the redirect ">" means PowerShell does the file writing and it uses UTF16 which doesn't play nice with psql. The -f flag makes pg_dump do the writing itself.

How to type text in CMD with batch file? [duplicate]

This question already has answers here:
How do I specify a password to 'psql' non-interactively?
(11 answers)
Postgresql: Scripting psql execution with password
(17 answers)
Run a PostgreSQL .sql file using command line arguments
(16 answers)
Closed 2 years ago.
Sorry if my question sounds a little bit obvious but I'm a little bit new on creating batch files, so the problem is the next one:
I'm creating a batch file that would to be open CMD and then go to a specific folder (psql) in order to execute some commands witch allow me to load data to a DataBase from a file (.csv)
This is my try:
cd "C:\pgsql\bin"
psql -h suggestedorder.postgres.database.azure.com -d DataAnalytics -U dev_ext#suggestedorder -c "\copy planning.prueba (centro, almacen, fecha_carga)from 'C:\Users\geradiaz.MODELO\Desktop\Envase\Selección_Envase\Inputs\No_Seleccionado\o.csv' with delimiter as ','
MyPassword
As I've said the first line goes to pgsql folder, the second line execute the command to copy the data to the data base and the last one (MyPassword) supposes to would be writing my password in the next line when CMD ask for it, but it seems like this last line does not work, when I execute the batch it just keeps the screen with the message "Password for User...".
Do you know guys with kind of commands do I need to execute this batch file?
There is no way to pass the password as an argument in psql, but you can try using the PGPASSWORD environment variable.
The script would be something like this:
#echo off
C:
cd \pgsql\bin
set PGPASSWORD=MyPassword
psql -h suggestedorder.postgres.database.azure.com -d DataAnalytics -U dev_ext#suggestedorder -c ....
For further info, see here: https://www.postgresql.org/docs/9.0/libpq-envars.html

How can we import the contents of a database to another database with command in PostgreSQL? [duplicate]

This question already has answers here:
PostgreSQL pg_dump syntax error in Ubuntu
(1 answer)
How to back up a postgresql database from within psql?
(2 answers)
Closed 3 years ago.
I think first we should use "pg_dump for create a CSV file,but I can not do that.
I Have a table which name is skill_level in database=postgres and I want to import that to my database=sportner
I use:
pg_dump -t skill_level postgres | psql sportner
but I have error in SQL shell : Syntax error at or near pg_dump.
I can move the csv file to my second database with this command:
COPY persons
FROM 'C:\person.csv' DELIMITER ',' CSV HEADER;
and it works.
I want do them just with SQL commands not with import/export in pgAdmin4.

Unable to restore BSON data into cosmos collection using mongorestore [duplicate]

This question already has answers here:
Restore data Mongodb from JSON
(2 answers)
Closed 4 years ago.
I have exported data from Azure CosmosDB collection using below command,
mongoexport -h xyz.documents.azure.com:10255 -u DB -p password -d DB
-c Product --ssl --sslAllowInvalidCertificates -o backup.bson
Now im restoring it to same collection using below command,
mongorestore -h xyz.documents.azure.com:10255 -u user1 -p password -d
DB -c Product --ssl --sslAllowInvalidCertificates --dir backup.bson
--numParallelCollections=1
However getting below error,
Failed: xyz.Product: error restoring from backup.bson: reading bson
input: invalid BSONSize: 1767842427 bytes
Just to close this question properly: The issue is that you're exporting and importing with different tools.
Either use:
mongoexport + mongoimport (which writes and reads JSON or CSV)
mongodump + mongorestore (which writes & reads a binary file).
You can't mix the two, and that was the root cause of your error, exporting as text and trying to import as BSON.
Looks like possible duplicate of mongorestore fails due to invalid BSONSize . Can you check the link and try once again?

put automatically password of user in pg_restore command line windows [duplicate]

This question already has answers here:
How do I specify a password to 'psql' non-interactively?
(11 answers)
Closed 4 years ago.
I want to restore my database postgres using pg_restore in command line and i want to put the password of user directly in the command, I tried this command but doesn't work
pg_restore -h localhost -p 5432 -U postgres -W 123 -d my_db my_backup.backup
but the following message is displayed
pg_restore: too many arguments on the command line (the first being "-d")
Set the password as an environment variable: set "PGPASSWORD=123"
You can set other arguments this way as well: https://www.postgresql.org/docs/current/static/libpq-envars.html
-W is to force a password prompt, not to supply a password directly, thats why it says there are too many arguments.
Putting it all together:
set "PGPASSWORD=123"
pg_restore -h localhost -p 5432 -U postgres -d my_db my_backup.backup
Update: Thanks #aschipfl, I initially had incorrect quoting on set