How make sqlcmd hold file access rights on linux? - sql-server

Just installed sql-server on Ubuntu 16.04, 2 days ago. Using sqlcmd for bulk insert i got:
Msg 4860, Level 16, State 1, Line 6 Cannot bulk load. The file
"~/test_data.txt" does not exist or you don't have file access rights.
Yes the file did exist, i made sure of it using the command cat.
Then i tried bcp tool, but i got:
SQLState = S1000, NativeError = 0 Error = [Microsoft][ODBC Driver 13
for SQL Server]Unable to open BCP host data-file
Also tried installing visual studio code and adding mssql extension but i got the same "file access rights" warning. And already used the chmod 777 trying to fix it. Didn't work.
Command bulk insert sqlcmd:
BULK INSERT TestEmployees FROM '~/test_data.txt'
WITH(
rowterminator = ','
);
Command on bcp tool
bcp auth in path/auth2.tsv -S localhost -U sa -P <my password> -d Trabalho1BD -c

I think your problem is the '~/test_data.txt' part of your bulk insert command. Specifically, that says "find a file called test_data.txt in the home directory". But whose home directory? Not yours! It's looking for the file in the home directory of the account that's running your SQL Server. Try changing that to a full path (i.e. '/home/«username»/test_data.txt' and that should do it.

Related

BCP Fails to import csv into Azure SQL database table

I'm getting the error code shown far below when I run the BCP command to import a CSV to an Azure hosted SQL existing table.
I can successfully test the connection using an ODBC user dsn from my PC
There is no ) in my csv file
I've tried with and without headers
I've tried converting the csv to a tab delim file
None of these work and after an two hours searching google, I could use some help...
BCP Command:
bcp us_equities_price_capture in "c:\temp\dev\sql\us_equities_price_capture4.csv" -S mydbserver1.database.windows.net -d mydbinstance -U sql_user1 -P mypassword -q -c -t -h
Error
SQLState = 37000, NativeError = 102
Error = [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near ')'.

AWS RDS SQL Server RESTORE ISSUE

I created SQL Server res instance on AWS. I want to restore the database from a .bak file. I performed below steps in order to restore:
sqlcmd -S XXX.rds.amazonaws.com -U root -P XXXX
USE master
go
create database xyzzy
RESTORE DATABASE xyzzy
FROM DISK = N'C:\Users\Administrator\Downloads\FMAT_CPS_backup_2017_09_29_040005_9422965.bak'
go
but I got this error
Msg 3110, Level 14, State 1, Server EC2AMAZ-BUJEKP3, Line 1
User does not have permission to RESTORE database 'catalyst'.
Msg 3013, Level 16, State 1, Server EC2AMAZ-BUJEKP3, Line 1
RESTORE DATABASE is terminating abnormally.
When I tried this command
C:\Users\Administrator>sqlcmd -e -S xxxxrds.amazonaws.com -U root -P xxxx -i "C:\Users\Administrator\Downloads\FMAT_CPS_backup_2017_09_29_040005_9422965.bak"
I got another error:
Sqlcmd: Error: Syntax error at line 224 near command 'S' in file 'C:\Users\Administrator\Downloads\FMAT_CPS_backup_2017_09_29_040005_9422965.bak'.
The .bak file is of 25GB size, I'm not able to open it.
How to resolve these errors?
You might have in-sufficient Permission do you proper IAM Credentials.
Alternatively
You can upload the file to S3 and Restore to RDS

How do you execute large SQL Server script file?

I am trying to execute a large SQL Server script file from the command prompt as its not loading in Management Studio. I am using this command
sqlcmd -S .\SQLEXPRESS -U ttandel -P ''
-d [Zen.Databases.Suite]
-i D:\NewMachine\COM.B_Address.Table.sql
Note: password is blank. I have tried all the options for keeping password as blank such as ("",'', ) but nothing is working.
Can anyone please suggest how to do this?
From comment:
I tried with this
sqlcmd -S .\SQLEXPRESS -E
-d [Zen.Databases.Suite]
-i D:\NewMachine\COM.B_Address.Table.sql
This is throwing an error
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'NAPG\ttandel'..
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Cannot open database "[Zen.Databases.Suite]" requested by the login. The login failed..
My Server Security information as below.
The following SQL command will do the work.I have used it multiple times for installing 100 million to 200 million records in DB.
sqlcmd -S Krishneil-PC -E -i C:\Users\Krishneil\Desktop\Script.sql
change Krishneil to your suitability.
Change -d [Zen.Databases.Suite] to -d "Zen.Databases.Suite"

Mysql import database

I am new to mysql.I got a .sql file which I needed to import,so I searched online and got the below command.It worked perfectly.
C:\Program Files\MySQL\MySQL Server 5.6\bin>mysql -u root -p test3 < test3.sql
But when I said use test3 from mysql command line client,it gave me this error :
ERROR 1049 (42000): Unknown database 'test3'
Am I missing some step?
The database needs to exist, and be called from the command line using -D
so within MySQL try;
mysql> create database test3;
and back at the command prompt, try;
C:\Program Files\MySQL\MySQL Server 5.6\bin>mysql -u root -p -D test3 < test3.sql
First of all you would need to create the database in MYSQL so that once you use the command, it is able to locate the name of the database
You can create the table using MYSQL developer tool or using the command line and then you can execute
mysql -u root -p test3 < test3.sql
You have not created test3 database yet. First create a test3 database & then try to import.

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