• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar

Coder Tutorial

  • Home
  • HTML
  • CSS
  • PHP
  • SQL
  • MySQL
  • JS
  • PL/SQL
  • Python
  • Java
  • Oracle

PostgreSQL

PostgreSQL ERROR: cannot begin/end transactions in PL/pgSQL

Cannot begin/end transactions in PL/pgSQL

Rollback

The cause of error: Cannot begin/end transactions in PL/pgSQL is the rollback command.
The solution is to use exception clause in the function.

Wrong function

CREATE OR REPLACE FUNCTION update_test
(p_old_name varchar, p_new_name varchar)
RETURNS varchar AS $$
DECLARE
 v_result varchar(250);
 v_count numeric:=0;
BEGIN
SELECT count(*) into v_count 
FROM test WHERE name = p_old_name;
if v_count > 1 then
 rollback; 
else 
 update test set name = p_new_name 
 where name = p_old_name;
end if;
return v_result;
END;
$$ LANGUAGE plpgsql;
select update_test('name_1', 'name_3');

ERROR: cannot begin/end
transactions in PL/pgSQL

HINT: use a begin block with
an exception clause instead.

Correct function

CREATE OR REPLACE FUNCTION update_test
(p_old_name varchar, p_new_name varchar)
RETURNS varchar AS $$
DECLARE
 v_result varchar(250);
 v_count numeric:=0;
BEGIN
SELECT count(*) into v_count 
FROM test WHERE name = p_old_name;
if v_count > 1 then
 rollback; 
else 
 update test set name = p_new_name 
 where name = p_old_name;
end if;
return v_result;

exception when others then
begin
v_result:='DBA ERROR';
return v_result;
end;
END;
$$ LANGUAGE plpgsql;
select update_test('name_1', 'name_3');

Data Output: DBA ERROR

Filed Under: PostgreSQL

PostgreSQL Column must appear in the GROUP BY clause

Column must appear in the GROUP BY clause or be used in an aggregate function

GROUP BY clause

The cause of error: Column must appear in the GROUP BY clause or be used in an aggregate function
The solution is to add the GROUP BY clause with column used in the select.

Wrong query

select first_name, last_name, count(*) 
from test.students;

ERROR: column “students.first_name” must appear
in the GROUP BY clause or be used in an aggregate function
LINE 1: select first_name, last_name, count(*)

Query returned successfully in 421 msec.

Correct query

select first_name, last_name, count(*) 
from test.students 
group by first_name, last_name;

Filed Under: PostgreSQL

PostgreSQL Column specified more than once

Column specified more than once

Duplicate column

The cause of error: Column specified more than once.
The solution is to remove duplicate columns.

Wrong insert

INSERT INTO test.students 
(id, first_name, first_name) 
VALUES (6, 'Paul', 'Paul');

ERROR: column “first_name” specified more than once
LINE 1: INSERT INTO test.students (id, first_name, first_name) VALUE…

Query returned successfully in 430 msec.

Correct query

INSERT INTO test.students 
(id, first_name) 
VALUES (6, 'Paul');

INSERT 0 1

Query returned successfully in 393 msec.

Filed Under: PostgreSQL

PostgreSQL Create database, Alter database examples

Create postgresql database

Create database syntax, alter database examples, rename and drop.

Syntax

CREATE DATABASE db_name
[ [ WITH ] [ OWNER [=] username ]
	   [ TEMPLATE [=] template ]
	   [ ENCODING [=] encoding ]
	   [ LC_COLLATE [=] lc_collate ]
	   [ LC_CTYPE [=] lc_ctype ]
	   [ TABLESPACE [=] tablespace ]
	   [ CONNECTION LIMIT [=] connlimit ] ]

Example

CREATE DATABASE customers_db;

Create a database customers owned by user user_1 with a default tablespace of customers_space:

CREATE DATABASE customers_db 
OWNER user_1 TABLESPACE customers_space;

Alter postgresql database

Syntax

ALTER DATABASE old_db_name RENAME TO new_db_name;
ALTER DATABASE db_name OWNER TO new_owner;
ALTER DATABASE db_name SET TABLESPACE new_tablespace;

Example

ALTER DATABASE customers_db 
	RENAME TO customers_db2;

Drop postgresql database

Syntax

Drop DATABASE [ IF EXISTS ] db_name;

Example

DROP DATABASE customers_db;

Filed Under: PostgreSQL

PostgreSQL Create schema syntax, Alter schema

Create postgresql schema

Create schema syntax, Alter schema with examples.

Syntax

CREATE SCHEMA schema_name 
[ schema_element [ ... ] ];

CREATE SCHEMA AUTHORIZATION 
username [ schema_element [ ... ] ];

Example

CREATE SCHEMA test;

Create a schema for user user_1, 
and schema will be created 
automatically with the name _user_1:
CREATE SCHEMA AUTHORIZATION user_1;

Alter postgresql schema

Syntax

ALTER SCHEMA schema_name 
RENAME TO new_schema_name;

ALTER SCHEMA schema_name 
OWNER TO new_owner;

Example

ALTER SCHEMA test 
RENAME TO test_2;

ALTER SCHEMA test 
OWNER TO user_2;

Drop postgresql schema

Syntax

DROP SCHEMA [IF EXISTS] 
schema_name [, ...] 
[CASCADE|RESTRICT];

Example

DROP SCHEMA test CASCADE;

Filed Under: PostgreSQL

  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Interim pages omitted …
  • Go to page 14
  • Go to Next Page »

Primary Sidebar

Tutorials

  • HTML Tutorial
  • CSS Tutorial
  • PHP Tutorial
  • SQL Tutorial
  • MySQL Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • PL/SQL Tutorial
  • Java Tutorial
  • SQLPlus Tutorial
  • Oracle Tutorial
  • PostgreSQL Tutorial
  • Ruby Tutorial

Secondary Sidebar

Recent Posts

  • PostgreSQL ERROR: cannot begin/end transactions in PL/pgSQL
  • PostgreSQL Column must appear in the GROUP BY clause
  • PostgreSQL Column specified more than once
  • PostgreSQL Create database, Alter database examples
  • PostgreSQL Create schema syntax, Alter schema
  • PostgreSQL Create database user, alter and drop username
  • PostgreSQL Alter table name. Modify column name
  • PostgreSQL Alter Function Examples
  • PostgreSQL Drop function syntax and example
  • PostgreSQL Alter Trigger, Disable trigger, enable
  • PostgreSQL Drop Trigger, syntax and example
  • PostgreSQL Create table type, alter and drop type
  • PostgreSQL Loop – End Loop
  • PostgreSQL Case – When – Then
  • PostgreSQL IF – Elsif – Else syntax
  • PostgreSQL IF – Then – Else syntax
  • PostgreSQL IF – Then syntax
  • PostgreSQL Control Structures syntax: if then else case
  • PostgreSQL Alter table add primary key to existing table
  • PostgreSQL Alter table add foreign key constraint to a table
  • PostgreSQL Alter table add unique key constraint to a table
  • PostgreSQL Alter table add column to existing table
  • PostgreSQL Alter table add multiple columns to existing table
  • PostgreSQL Alter table drop column from existing table
  • PostgreSQL Alter table drop multiple columns from table
  • PostgreSQL Alter table rename column from existing table
  • PostgreSQL Alter table rename table name
  • PostgreSQL Alter table add check constraint to a table
  • PostgreSQL Alter table drop check constraint from a table
  • PostgreSQL tutorial. Error messages help: cause and solution.

Copyright  2018 - 2021 Coder Tutorial

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Non-necessary

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

SAVE & ACCEPT