can't use postgresql function in bash script - sql

I am new to postgresql and I'm using postgresql 9.3 and postgis 2.1.
In pgadmin, I am able to use a function in database "AddGeometryColumn", but when I write that script in Ubuntu bash, it doesn't work. The error I got is
ERROR: function addgeometrycolumn(unknown, unknown, unknown, unknown, unknown, integer) does not exist
LINE 1: SELECT AddGeometryColumn('tiger_staging','al_place','the_geo...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Does anyone have similar experience?
Also how do you specify "use" database in postgresql? like a query in mysql "use mydatabase" to switch database?

I presume you are using "psql", so do just like this:
psql [databasename] -U [username] -t -e --command="select [function]"
As "Hint" says, you need to cast your param values with exact same types of your function's parameters:
psql [databasename] -U [username] -t -e --command="SELECT AddGeometryColumn(cast('tiger_staging' as character varying),..."
Also check if the number of params in your select matchs your function's parameters.

You are probably connected to a database that does not have the PostGIS extension installed (e.g. the default postgres database). In psql (the command-line client), check that the name coming before the =# prompt matches the database you want to use:
postgres=#
To switch to another database, use the \c command:
postgres=# \c mydatabase
...
mydatabase=#
To check that the PostGIS extension is correctly installed in your current DB, use the postgis_full_version() function:
SELECT postgis_full_version();
This should return information on the PostGIS version if the extension is enabled. If you get a No function matches the given name and argument types error, you will need to install the extension in your current DB:
CREATE EXTENSION postgis;
Note that the PostGIS package has to be installed in order to be able to enable the extension in a database.

You are tripping over CaMeL case names.
Identifier (like AddGeometryColumn) are folded to lower case (addgeometrycolumn) unless double-quoted ("AddGeometryColumn"). Details in the manual.
Unfortunately, PostGis functions follow a naming convention that requires double-quoting at all times.
For your bash-invocation, you probably need to use single quotes around your string to remove the special meaning of double-quotes. Or escape with backslash (\").
Where ever I can I stick to legal, lower-case names exclusively for all identifiers to prevent problems like the one at hand.

Related

Correct VBA Syntax for DB2 Command?

I'm attempting to connect to DB2 through VBA. I have a connection established through the ODBC provider.
Here's the string of my command text looks like:
strCmd = "INSERT INTO mySchema.myTable (Text) VALUES ('Test')"
When I run this, I get the following runtime error:
[IBM][CLI Driver][DB2/NT64] SQL0204N "MYSCHEMA.MYTABLE" is an undefined name. SQLSTATE=42704
I have validated and verified that the schema and table exist in DB2. I have validated (by using another tool - IBM Data Studio - that the credentials have access and authority to write to this table.
Is my syntax wrong? Is there something I'm missing? If I don't add the "MYSCHEMA." in front of the table name, it presumes I want the "ADMIN" schema, which I don't (it doesn't even exist).
How do I successfully execute an insert command to DB2 LUW?
This is frequently asked.
Db2 automatically folds unquoted object names into uppercase.
This makes programming easier because being forced to quote objects is not so friendly.
It means that "myTable"."mySchema" is a different object than MYTABLE.MYSCHEMA.
So in general it is easier to configure your toolset to not quote object names when creating the objects, and so allow them to be folded to uppercase. It also allows subsequent queries to avoid having to quote table names and column names.
But sometimes you don't have a choice.

What does "SELECT INTO" do?

I'm reading sql code which has a line that looks like this:
SELECT INTO _user tag FROM login.user WHERE id = util.uuid_to_int(_user_id)::oid;
What exactly does this do? The usual way to use SELECT INTO requires specifying the columns to select after the SELECT token, e.g.
SELECT * INTO _my_new_table WHERE ...;
The database is postgresql.
This line must appear inside of a PL/pgSQL function. In that context the value from column tag is assigned to variable _user.
According to the documentation:
Tip: Note that this interpretation of SELECT with INTO is quite different from PostgreSQL's regular SELECT INTO command, wherein the INTO target is a newly created table.
and
The INTO clause can appear almost anywhere in the SQL command. Customarily it is written either just before or just after the list of select_expressions in a SELECT command, or at the end of the command for other command types. It is recommended that you follow this convention in case the PL/pgSQL parser becomes stricter in future versions.

Postgres Case Sensitivity

I have imported 100 of tables in Postgres from MSSql server 2008 through tool which created all the tables along with their columns in capital letter. Now if I want to make a data view from table e.g - STD_TYPE_CODES as-
select * from STD_TYPE_CODES
I am getting following error-
ERROR: relation "std_type_codes" does not exist
LINE 1: select * from STD_TYPE_CODES
^
********** Error **********
ERROR: relation "std_type_codes" does not exist
SQL state: 42P01
Character: 15
I know I can put the quotes around the table name as-
select * from "STD_TYPE_CODES"
But as I have worked with MSSql Server, there is no such kind of issue.
So is there any way to get rid of this? Please help.
In PostgreSQL unquoted names are case-insensitive. Thus SELECT * FROM hello and SELECT * FROM HELLO are equivalent.
However, quoted names are case-sensitive. SELECT * FROM "hello" is not equivalent to SELECT * FROM "HELLO".
To make a "bridge" between quoted names and unquoted names, unquoted names are implicitly lowercased, thus hello, HELLO and HeLLo are equivalent to "hello", but not to "HELLO" or "HeLLo" (OOPS!).
Thus, when creating entities (tables, views, procedures, etc) in PostgreSQL, you should specify them either unquoted, or quoted-but-lowercased.
To convert existing tables/views/etc you can use something like ALTER TABLE "FOO" RENAME TO "foo".
Or, try to modify dump from MSSQL to make it "PostgreSQL-compatible" (so that it will contain foos or "foo"s but not "FOO"s).
Either by explicitly editing dump file. (If you're using Linux, you can do sed -r 's/"[^"]+"/\L\0/g' dumpfile — however be warned that this command may also modify text in string literals.)
Or by specifying some options when getting dump from MSSQL. (I'm not sure if there are such options in MSSQL, never used it, but probably such options should exist.)

How to indicate in postgreSQL command in which database to execute a script? (simmilar to SQL Server "use" command)

I have the following problem, I need to put in a script that is going to run before the new version is rolled the SQL code that enables the pgAgent in PostgreSQL. However, this code should be run on the maintenance database (postgres) and the database where we run the script file is another one.
I remember that in SQL Server there is a command "use " so you could do something like:
use foo
-- some code
use bar
-- more code
is there something similar in PostgreSQL?
You can put in your file something like:
\c first_db_name
select * from t; --- your sql
\c second_db_name
select * from t; --- your sql
...
Are you piping these commands through the psql command? If so, \c databasename is what you want.
psql documentation
You can't switch databases in Postgres in this way. You actually have to reconnect to the other database.
PostgreSQL doesn't have the USE command. You would most likely use psql with the --dbname option to accomplish this, --dbname takes the database name as a parameter. See this link for details on the other options you can pass in you will also want to check out the --file option as well. http://www.postgresql.org/docs/9.0/interactive/app-psql.html
well after looking on the web for some time I found this which was what I need it
http://www.postgresonline.com/journal/archives/44-Using-DbLink-to-access-other-PostgreSQL-Databases-and-Servers.html

How to see all the tables in an HSQLDB database?

I usually use SQLDeveloper to browse the database, but I couldn't make it work with HSQLDB and I don't know which tables are already created…
I guess it's a vendor-specific question and not plain SQL, but the point is: how can I see the tables so I can drop/alter them?
The ANSI SQL92 standard for querying database metadata is contained within the INFORMATION_SCHEMA data structures.
I have no idea whether your database supports this or not, but try the following:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
On further research, it appears that HSQLDB does support INFORMATION_SCHEMA, but with slightly non-standard naming.
All of the tables have SYSTEM_* prepended to them, so the above example would read
SELECT *
FROM INFORMATION_SCHEMA.SYSTEM_TABLES
I have no means of testing this, and the answer was found on sourceforge.
Awesome, thanks! Been scouring the Web for that info.
This will fetch only your tables' field info:
SELECT TABLE_NAME, COLUMN_NAME, TYPE_NAME, COLUMN_SIZE, DECIMAL_DIGITS, IS_NULLABLE FROM INFORMATION_SCHEMA.SYSTEM_COLUMNS WHERE TABLE_NAME NOT LIKE 'SYSTEM_%'
You can retrieve indexes, primary key info, all kinds of stuff from INFORMATION_SCHEMA.SYSTEM_TABLES.
Gotta love oo documentation :p
If you're on the command line, you may want to try the Hsqldb SqlTool, documented in the SqlTool Manual (hsqldb.org).
Put your database connection information in "~/sqltool.rc" and choose any DBNAME you want, substitute correct username and password, if known.
urlid DBNAME
url jdbc:hsqldb:/path/to/hsql/database
username SA
password
Install tool with: apt-get install hsqldb-utils (on Ubuntu)
Connect with hsqldb-sqltool DBNAME # on Ubuntu
Hint for other systems: java -jar YourHsqlJar.jar DBNAME
Show tables with: \dt
Show columns with: \d TABLENAME
Standard queries like: SELECT * FROM …;
Edit (append) last command with: :a
Quit with: \q
View special commands with: \? OR :?
Good luck!
Use the \dt command when you hit the >sql prompt in the command line for HSQLDB.
Check out DBVisualiser and SQuirreL SQL Client. Both of these have support for HSQLDB, and a GUI for editing/modifying/viewing the tables.
You run querying using hsql database manager, are you?
If you use this, below may give some hints:
Select your connection:
type: HSQL DATABASE ENGINE SERVER
Driver: jdbc.hsqldb.jdbcDriver
URL: jdbc:hsqldb:hsql://localhost/
Then, you will browse the database.