Return error number - postgresql

I want to return error number using PostgreSQL 9.3 Version.
I referred this :-> http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
but want to know how do I return the error number.
In SQL Server 2008 R2 we just use ##ERROR..
Example:
IF ##ERROR <> 0
BEGIN
PRINT 'Error message';
RETURN(1);
END
My Question: Can we get error number in PostgreSQL like we get in SQL Server by using ##ERROR?

In PL/pgSQL, you must trap errors and you can raise (custom) errors. (Normal execution is stopped during an error.)
Errors in PostgreSQL has levels, error codes (SQLSTATE) and explicit names.
F.ex:
BEGIN
-- code that can potentially raise an error
EXCEPTION
WHEN division_by_zero THEN -- trap by name
-- handle division_by_zero
WHEN SQLSTATE '22012' THEN -- trap by SQLSTATE
-- handle SQLSTATE 22012
WHEN OTHERS THEN -- trap all other error
RAISE no_data; -- raise error by name
RAISE SQLSTATE '02000'; -- raise error by SQLSTATE
END

Related

bucardo sync issue, function does not exist

I have issues with adding database to sync. DB versions: PostgreSQL 9.6.8 (RDS), Jump system: Ubuntu18.04 LTS, bucardo version: 5.4.1.
When trying run this command:
bucardo add sync db_sync relgroup=copying_herd dbs=source_db:source,dest_db:target onetimecopy=1
I receive this error:
WARNING: Issuing rollback() due to DESTROY without explicit disconnect() of DBD::Pg::db handle dbname=dc_sourcedb;host=xxx.aaa.xyz;port=5432 at line 126.
Failed to add sync: DBD::Pg::st execute failed: ERROR: DBD::Pg::db do failed: ERROR: function search_string(text) does not exist
LINE 5: array_agg(search_string(x)),
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
QUERY:
SELECT
coalesce(
array_to_string(
array_agg(search_string(x)),
'|'
)::text,
''
)
FROM unnest($1) x
CONTEXT: SQL function "flatten_text_array" during inlining at line 126. at line 30.
CONTEXT: PL/Perl function "validate_sync" at /usr/bin/bucardo line 4612.
Function search_string exist and I can run this function from psql command line

Try .... Catch is not giving all the errors in T-SQL execution

I am trying to restore a database from a backup using Try Catch block. In that I am using first restore file list and then restore backup using Move option. with Try ... Catch Block. Iam trying to catch those errors into a table with insert statement. In that table I am getting only one message...
Restore Filelist terminating abnormally
ErrorNumber ErrorSeverity ErrorState ErrorProcedure ErrorLine ErrorMessage
----------- ------------- ----------- --------------- ---------- ----------------------------------
3013 16 1 NULL 1 RESTORE FILELIST is terminating abnormally..
but not the following message...
Msg 3201, Level 16, State 2, Line 1
Cannot open backup device '\\SQLBKUP\SQL Server\SQLInstance\DEFAULT\XYZ_9389451.bak'.
Operating system error 2(The system cannot find the file specified.).
Any thoughts why I am NOT getting that error captured with the Catch Block?

error when running anonymous block

running into the below error when trying to run a simple anonymous block
do $$ begin raise notice 'Hello, World!'; end $$;
error
Describe Error: Failed to retrieve EXPLAIN plan(s): ERROR: syntax
error at or near "do"
DB is PostgreSQL 9.1.18 on x86_64-unknown-linux-gnu
any ideas on what is going wrong?
I am using aqua data studio and pgadmin and get the same error

MAMP mysql update fails

When I try to start my mysql and apache servers through MAMP on macOS, I get a message every time saying that mysql needs to be updated.
I try to update mysql through the MAMP interface, but it errors out with this:
Warning: Using a password on the command line interface can be insecure.
Looking for 'mysql' as: /Applications/MAMP/Library/bin/mysql
Looking for 'mysqlcheck' as: /Applications/MAMP/Library/bin/mysqlcheck
Error: Failed while fetching Server version! Could be due to unauthorized access.
FATAL ERROR: Upgrade failed
How do I fix this so that I can start my servers properly?
EDIT for clarity: I appreciate the link to the other thread. Very similar but I still need help with getting MAMP to update mysql. When I try /Applications/MAMP/Library/bin/mysql_upgrade --port=3306 -u root -p in the terminal as suggested in that post I get:
Looking for 'mysql' as: /Applications/MAMP/Library/bin/mysql
Looking for 'mysqlcheck' as: /Applications/MAMP/Library/bin/mysqlcheck
Running 'mysqlcheck' with connection arguments: '--port=3306'
Warning: Using a password on the command line interface can be insecure.
Running 'mysqlcheck' with connection arguments: '--port=3306'
Warning: Using a password on the command line interface can be insecure.
mysql.columns_priv OK
mysql.db OK
mysql.event OK
mysql.func OK
mysql.general_log OK
mysql.help_category OK
mysql.help_keyword OK
mysql.help_relation OK
mysql.help_topic OK
mysql.innodb_index_stats
Error : Table 'mysql.innodb_index_stats' doesn't exist
status : Operation failed
mysql.innodb_table_stats
Error : Table 'mysql.innodb_table_stats' doesn't exist
status : Operation failed
mysql.ndb_binlog_index OK
mysql.plugin OK
mysql.proc OK
mysql.procs_priv OK
mysql.proxies_priv OK
mysql.servers OK
mysql.slave_master_info
Error : Table 'mysql.slave_master_info' doesn't exist
status : Operation failed
mysql.slave_relay_log_info
Error : Table 'mysql.slave_relay_log_info' doesn't exist
status : Operation failed
mysql.slave_worker_info
Error : Table 'mysql.slave_worker_info' doesn't exist
status : Operation failed
mysql.slow_log OK
mysql.tables_priv OK
mysql.time_zone OK
mysql.time_zone_leap_second OK
mysql.time_zone_name OK
mysql.time_zone_transition OK
mysql.time_zone_transition_type OK
mysql.user OK
Repairing tables
mysql.innodb_index_stats
Error : Table 'mysql.innodb_index_stats' doesn't exist
status : Operation failed
mysql.innodb_table_stats
Error : Table 'mysql.innodb_table_stats' doesn't exist
status : Operation failed
mysql.slave_master_info
Error : Table 'mysql.slave_master_info' doesn't exist
status : Operation failed
mysql.slave_relay_log_info
Error : Table 'mysql.slave_relay_log_info' doesn't exist
status : Operation failed
mysql.slave_worker_info
Error : Table 'mysql.slave_worker_info' doesn't exist
status : Operation failed
Running 'mysql_fix_privilege_tables'...
Warning: Using a password on the command line interface can be insecure.
ERROR 1146 (42S02) at line 62: Table 'mysql.innodb_table_stats' doesn't exist
ERROR 1243 (HY000) at line 63: Unknown prepared statement handler (stmt) given to EXECUTE
ERROR 1243 (HY000) at line 64: Unknown prepared statement handler (stmt) given to DEALLOCATE PREPARE
ERROR 1146 (42S02) at line 66: Table 'mysql.innodb_index_stats' doesn't exist
ERROR 1243 (HY000) at line 67: Unknown prepared statement handler (stmt) given to EXECUTE
ERROR 1243 (HY000) at line 68: Unknown prepared statement handler (stmt) given to DEALLOCATE PREPARE
ERROR 1146 (42S02) at line 81: Table 'mysql.slave_relay_log_info' doesn't exist
ERROR 1243 (HY000) at line 82: Unknown prepared statement handler (stmt) given to EXECUTE
ERROR 1243 (HY000) at line 83: Unknown prepared statement handler (stmt) given to DEALLOCATE PREPARE
ERROR 1146 (42S02) at line 110: Table 'mysql.slave_master_info' doesn't exist
ERROR 1243 (HY000) at line 111: Unknown prepared statement handler (stmt) given to EXECUTE
ERROR 1243 (HY000) at line 112: Unknown prepared statement handler (stmt) given to DEALLOCATE PREPARE
ERROR 1146 (42S02) at line 128: Table 'mysql.slave_worker_info' doesn't exist
ERROR 1243 (HY000) at line 129: Unknown prepared statement handler (stmt) given to EXECUTE
ERROR 1243 (HY000) at line 130: Unknown prepared statement handler (stmt) given to DEALLOCATE PREPARE
ERROR 1146 (42S02) at line 1896: Table 'mysql.slave_master_info' doesn't exist
ERROR 1146 (42S02) at line 1897: Table 'mysql.slave_master_info' doesn't exist
ERROR 1146 (42S02) at line 1898: Table 'mysql.slave_master_info' doesn't exist
ERROR 1146 (42S02) at line 1899: Table 'mysql.slave_worker_info' doesn't exist
ERROR 1146 (42S02) at line 1900: Table 'mysql.slave_relay_log_info' doesn't exist
ERROR 1146 (42S02) at line 1904: Table 'mysql.innodb_table_stats' doesn't exist
ERROR 1146 (42S02) at line 1908: Table 'mysql.innodb_index_stats' doesn't exist
FATAL ERROR: Upgrade failed
I also tried finding the my.cnf file for MAMP and backing it up as someone else in the other thread suggested but could not find it. If someone would not mind pointing out to me where it is, so that I can see if updating mysql after that works.
I will also add that I am using MAMP version 4.2 on macOS 10.12. If that helps any.

PostgreSQL plperlu compilation error: ERROR: didn't get a CODE reference from compiling function

I have created language plperlu in Postgres 9.3 (OS - UBUNTU 12.04.5), using below command
CREATE LANGUAGE plperlu;
I wanted to call shell script from postgres function So I written below function using plperlu.
Below is function
CREATE OR REPLACE FUNCTION tet_func (integer) RETURNS boolean
AS $$
$checkexitcode = system
("sh test_shell.sh");
if ($checkexitcode > 0) { return false; }
return true;
$$ LANGUAGE plperlu;
While compiling I am getting below error
ERROR: didn't get a CODE reference from compiling function "tet_func"
CONTEXT: compilation of PL/Perl function "tet_func"
********** Error **********
ERROR: didn't get a CODE reference from compiling function "tet_func"
SQL state: XX000
Context: compilation of PL/Perl function "tet_func"
Does anybody have idea why this error is occurring? Or anyone has other solution to call shell script from postgres function.