Isql command error while executing query on Sybase database - sybase

I would like to create a Windows batch file to execute the query on Sybase database running on Linux.
Batch file:
plink.exe -ssh sybase#<IP> -pw <PW> -m C:\scripts\script1.bat -t > C:\scripts\testing.log
script1.bat:
echo --- query 1 -----
cd /sybase/OCS-15_0/bin/
isql -Uimaldb_bkp -Pstart_bkp -SLinux1 -Dimaldb
sp_helpsegment
go
exit
It works fine until isql command and gives output in testing.log:
--- query 1 ----- bash: isql: command not found bash: sp_helpsegment: command not found bash: go: command not found
Please advice.

'isql' will work once the required environment variables for Sybase ASE are set in the database server (Linux server as mentioned by you). Please check the link below for more details:
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc35823.1570/html/uconfig/X30690.htm
On a simpler note, there should be a file named 'SYBASE.sh' inside '/sybase' (I guess Sybase has been installed in this directory from your sample code). You need to source this file by editing the '.bashrc' file present inside the home directory of the user you are using to connect to the Linux server.
For the sql to work, you will need a flag to indicate the start and end of sql block in the script. Please try the following:
isql -Uimaldb_bkp -Pstart_bkp -SLinux1 -Dimaldb <<EOF
sp_helpsegment
go
EOF
You can use any other word instead of 'EOF'

I too face the same issue.
You need to insert your sybase environment variables, like below:
. /sybase/ABD/SYBASE.sh
isql -U**** -P****** -S<SID> -X <<EOF
It will work fine. Please try this.

If you don't have permission to change de server PATH, you can find the path of isql and execute this way:
/sybase/OCS-15_0/bin/isql -Uimaldb_bkp -Pstart_bkp -SLinux1 -Dimaldb
Putting the prefix PATH of isql installation (it depends of your version and path)

Related

Linux cli tool to dump SQL Server schema to a text file

Do you know a reliable command line tool able to export SQL Server schema to a text file?
You can do this with mssql-scripter. Download through - pip install mssql-scripter.
The command you'll want to use is along the lines of:
$ mssql-scripter -S serverName -d databaseName -U user > ./my-schema.sql
The default is schema only (you can also specify --schema-and-data and --data-only). The command line will prompt for your password.
And you can pipe to stdout, sed, or a .sql file currently. Here's the GitHub page as this is an OSS repo - https://github.com/Microsoft/mssql-scripter. Please do file issues on the repo if you run into any.

How do I create a batch file from Advantage SQL script?

I have created sql script using SQL Utility within Advantage Data Architect of Sybase. The script is saved on my workstation. Now, how do I create a batch file that would run the script from desktop?
I found this command line online, but it doesn't seem to be working:
#echo on
isql -U "username" -P "password" -S "servername" -D "database" -i "path"
#echo off
I am new to Advantage SQL, trying to learn as much as I can.
Thanks.
You use adssqlcmd.exe, passing the connection and script name as command-line parameters. There is an example of doing so on that second page:
rem Using the connection path option, and process the script files
rem after making the connection.
rem The program will terminate after processing all files
asqlcmd.exe -S ALS:d:\mydata\main.add -U user1 -P sample -i myscript.sql
There's a list of supported commands that outline what you can and can't do in the script as well.
Also note that adssqlcmd.exe is a feature added in ADS v11, and is not available to earlier versions of ADS.

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.

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

How to update database with script

I need to create Postgre DB in Windows. So I've downloaded the windows tools from the official site, created server and the problem appeared. When I try to create db in pgAdmin III I'm getting syntax errors while copying data. So I need to run the whole thing in the console. But pgAdmin only allows console mode for db already created and when I run it I have shell for my empty database:
dbname->
How can I now run my script ?
Have you tried using psql.exe? Given psql.exe is on your path try:
c:\> psql.exe -h localhost -U username -f c:\mysqlscript.sql database_name
In windows try this:
\i c:/somedir/script2.sql

Resources