Using squirrel sql with postgresql: backslash syntax error? - postgresql

I am trying to run basic postgresql commands which start with a backslash within Squirrel SQL sql client. For example, I'd like to be able to type
\dt
to mean "SHOW TABLES" instead of
"SELECT * FROM information_schema.tables WHERE table_schema = 'public';"
This works from the psql command line. However, when I try to run "\dt" within Squirrel I get a syntax error message:
Error: ERROR: syntax error at or near "\"
Position: 1
SQLState: 42601
ErrorCode: 0
I assume there's some kind of SQL syntax checking going on here on the part of Squirrel? Does any one know a way to make PostgreSQL commands which start with a backslash work in Squirrel SQL? I have the Postgres plugin installed...
Thanks,

The backslash commands are part of the psql client, not the PostgreSQL backend server. psql translates them into batches of SQL, which you can see by running psql with the -E flag, and uses the results to produce the displayed output.
This means you can't use these commands from other clients.
Alongside the inability to use pg_dump from within a PostgreSQL protocol session, or get equivalent functionality from the backend server, this is a bit of an FAQ.
At this point the only real option is to use a client that understands the information_schema or PostgreSQL's catalogs (pg_catalog) and can produce the display you want its self. A popular choice is PgAdmin-III, though I stick with psql myself.

Related

SQLCODE=-104, SQLSTATE=42601, SQLERRMC=table;reorg ;JOIN <joined_table>

when running this query on db2 on DBeaver :
reorg table departments
i got this error (just on external channel):
DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=table;reorg ;JOIN <joined_table>, DRIVER=4.19.49
what does this query mean?
how can I fix the error?
appricicate any help.
Try call sysproc.admin_cmd('reorg table db2inst1.departments')
as you are using DBeaver which is a jdbc application.
If you do not qualify the table name (for example, with db2inst1) then Db2 will assume that the qualifier (schema name) is the same as the userid name you used when connecting to the database.
DBeaver runs SQL statements, but it cannot directly run commands of Db2 - instead, any jdbc app can run Db2-commands indirectly via a stored-procedure that you CALL. The CALL is an SQL statement.
The reorg table is a command, it is not an SQL statement, so it needs to be run via the admin_cmd stored-procedure, or it can be run from the operating system command line (or db2 clp) after connecting.
So if you have db2cmd.exe on MS-Windows, or bash on linux/unix, you can connect to the database, and run commands via the db2 command.

ERM from PostgreSQL via psql (SQL Shell)

I need to understand the relations between tables in a PostgreSQL database. I will not have the ability to download pgAdmin4 like I am used to working in. So after looking around I found pg_dump.exe built into PostgreSQL. I thought I could just do something like this in the SQL Shell (psql) to get a dump of the database and then have the ability to upload it into another system:
database=# pg_dump database > path/to/save/file.sql;
Based on the docs >> https://www.postgresql.org/docs/9.3/app-pgdump.html
But when I run that I get an error:
ERROR: syntax error at or near "pg_dump"
LINE 1: pg_dump database > path/to/save/file.sql;
^
I saw on this stack overflow question was similar to my issue with the pg_dump error. In the solutions, Adrian says that pg_dump does not work within psql. When I run the code that Adrian suggested, I also get an error.
Any thoughts on how I can use psql to get the information that I need of this database?
Note: I am accessing a Linux VM on a Windows machine.

run \dt in oracle sqldeveloper for PostgreSQL

I configured PostgreSQL for Oracle SQL developer.
I just tried to run the command \dt; from the worksheet and received below error
Error starting at line : 1 in command -
\dt
Error report -
SQL Error: ERROR: syntax error at or near "\"
Position: 1
Could you please advise how to run the commonly used psql commands from worksheet in Oracle SQL developer.
Edit1:
Are there any alternative equivalent for such psql command for oracle sql developer please. Thanks.
Doesn't SQL Developer offer a GUI view that provides the same info as \dt does? That is its job, afterall, and the point of using a GUI.
You can start psql with the -E option, then it will show you the SQL it executes to get the data behind the various backslash commands. You can then capture and run those queries in something else, like (presumably, I haven't tried it) SQL Developer.
You cannot do that.
psql commands are only available in psql.
Consider using psql, it is superior.

Export ixf in db2

EXPORT TO myFile.ixf OF ixf SELECT * FROM TABLE_NAME WHERE SSN='DATA' AND EMPLOYER_ID=DATA AND CREATED_TS='DATA'
I am using this statement to export a couple of rows. for privacy purposes DATA has been inserted where necessary. however the following error is produced. I have followed IBM's guide on export and feel like this should be correct but unsure exactly as to what is wrong. the error log is as follows
Error: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=myFile;EXPORT TO ;JOIN, DRIVER=3.53.70
SQLState: 42601
ErrorCode: -104
As already remarked, you cannot directly run Db2-commands (such as import, export, load ... etc.) from plain SQL , as you are trying to do via JDBC.
Instead, if your Db2-server runs on Linux/Unix/Windows, you can either use a stored procedure, or (for any Db2-server operating system) you can use the command-line.
However, when you use stored-procedure SYSPROC.ADMIN_CMD for Db2-LUW, all file-names in stored-procedure parameters are relative to the Db2-server (and not your remote jdbc-client, if you are running remotely).
That means after a successful export via stored-procedure, if you really need the exported IXF file to be on your workstation then you must do file-transfer to your workstation using whatever tools you have for that purpose.
For example, this shows an export on Unix to an IXF file in /tmp on the Db2-server:
call sysproc.admin_cmd('EXPORT TO /tmp/myFile.ixf OF ixf SELECT * FROM user1.stk1 with ur') ;
If you don't want to use a stored procedure, you must use the command-line shell (for example on Windows, use db2ntcmd.bat , or on Unix use bash or ksh) and connect to the database in the shell and perform the export. This requires the workstation to have a Db2-client and also that the relevant database and node be first catalogued.
If you specify your Db2-version and the operating-system on which your Db2-server runs, then you will get more details.

PostgreSQL 9.4 \copy Error

I am attempting to copy data from a csv file into a postgresql database, and am using the following line to do so:
\copy album_tracks FROM 'C:\Users\wMogerty\Desktop\test.csv' WITH (format csv, header true);
However, I am getting the following error and am unsure why:
Error: ERROR: syntax error at or near "\"
Position: 1
SQLState: 42601
ErrorCode: 0
Thanks for any help
\copy is a command in the psql client. It is not supported by the PostgreSQL server. Nor is it part of libpq so it's not supported by alternative clients like PgAdmin-III etc.
You'll need to copy the file to the server yourself and use the COPY command to load it into the database. Or if your client has another interface to the underlying COPY ... FROM STDIN feature used by \copy, like an "import table" or "bulk load" option, you can use that.
I wonder if we should make the PostgreSQL server's parser detect attempts to run \copy as a SQL query and give a better error? Sounds like a great patch for a new developer to try.