diff --git a/src/test/regress/expected/database.out b/src/test/regress/expected/database.out
new file mode 100644
index 0000000..1f5e80e
--- /dev/null
+++ b/src/test/regress/expected/database.out
@@ -0,0 +1,140 @@
+--
+-- tests functions in dbcommands.c script
+--
+-- Should fail. Check for cases when DROP DATABASE is not allowed
+DROP DATABASE db_db2;   -- doesn't exist
+ERROR:  database "db_db2" does not exist
+DROP DATABASE IF EXISTS db_db2; -- doesn't exist with IF EXISTS;
+NOTICE:  database "db_db2" does not exist, skipping
+DROP DATABASE template1;    -- can't drop a template database
+ERROR:  cannot drop a template database
+DROP DATABASE regression;   -- can't drop a database in use
+ERROR:  cannot drop the currently open database
+-- Should fail. Can't CREATE DATABASE inside a transaction
+BEGIN TRANSACTION;
+CREATE DATABASE db_db3;
+ERROR:  CREATE DATABASE cannot run inside a transaction block
+ROLLBACK;
+-- Should fail. Can't DROP DATABASE inside a transaction
+BEGIN TRANSACTION;
+DROP DATABASE regression;
+ERROR:  DROP DATABASE cannot run inside a transaction block
+ROLLBACK;
+-- Should fail. ALTER DATABASE RENAME TO on invalid DB
+ALTER DATABASE db_db1_nonexistent RENAME TO db_db1a;   -- Database does not exist
+ERROR:  database "db_db1_nonexistent" does not exist
+ALTER DATABASE regression RENAME TO regression; -- Database can't be renamed to itself
+ERROR:  database "regression" already exists
+--ALTER DATABASE regression RENAME TO db_db1a;   -- Can't rename current database
+-- Should fail. ALTER DATABASE RENAME TO without permission
+CREATE ROLE regress_rol_db7;
+SET ROLE regress_rol_db7;
+ALTER DATABASE regression RENAME TO db_db7;
+ERROR:  must be owner of database regression
+RESET ROLE;
+DROP ROLE regress_rol_db7;
+-- Should fail. ALTER DATABASE RENAME TO with OWNER rights without CREATEDB
+BEGIN TRANSACTION;
+CREATE ROLE regress_rol_db8;
+ALTER DATABASE regression OWNER TO regress_rol_db8;
+SET ROLE regress_rol_db8;
+ALTER DATABASE regression RENAME TO db_db8a;
+ERROR:  permission denied to rename database
+ROLLBACK;
+-- Should fail. ALTER DATABASE SET / WITH with invalid values
+ALTER DATABASE regression SET invalid_parameter;
+ERROR:  syntax error at or near ";"
+LINE 1: ALTER DATABASE regression SET invalid_parameter;
+                                                       ^
+ALTER DATABASE regression WITH CONNECTION LIMIT = -100;
+ERROR:  invalid connection limit: -100
+ALTER DATABASE regression WITH CONNECTION LIMIT = 8ASDF;
+ERROR:  syntax error at or near "ASDF"
+LINE 1: ALTER DATABASE regression WITH CONNECTION LIMIT = 8ASDF;
+                                                           ^
+-- Should fail. ALTER DATABASE with redundant options
+ALTER DATABASE regression WITH CONNECTION LIMIT 1 CONNECTION LIMIT 2;
+ERROR:  conflicting or redundant options
+-- Should work. ALTER DATABASE SET / WITH with valid values
+ALTER DATABASE regression WITH CONNECTION LIMIT =  -1;
+ALTER DATABASE regression WITH CONNECTION LIMIT =  0;
+ALTER DATABASE regression WITH CONNECTION LIMIT =  100;
+ALTER DATABASE regression SET SEED = 0.5;
+-- Should fail. Can't set Tablespace within transaction
+BEGIN TRANSACTION;
+ALTER DATABASE regression SET TABLESPACE pg_default;
+ERROR:  ALTER DATABASE SET TABLESPACE cannot run inside a transaction block
+ROLLBACK;
+-- Should work. ALTER DATABASE OWNER TO requires CREATEDB rights for grantor
+BEGIN TRANSACTION;
+CREATE ROLE regress_rol_db15;
+CREATE ROLE regress_rol_db14 IN ROLE regress_rol_db15;  -- CREATE regress_rol_db14 to be a member of regress_rol_db15
+ALTER DATABASE regression OWNER TO regress_rol_db14;
+SET ROLE regress_rol_db14;
+ALTER DATABASE regression OWNER TO regress_rol_db15;
+ERROR:  permission denied to change owner of database
+ROLLBACK;
+-- Should fail. CREATE DATABASE with invalid values
+CREATE DATABASE db_db18 ENCODING 'invalid_encoding';
+ERROR:  invalid_encoding is not a valid encoding name
+CREATE DATABASE db_db18 ENCODING 123456789;
+ERROR:  123456789 is not a valid encoding code
+CREATE DATABASE db_db18 CONNECTION LIMIT -10;
+ERROR:  invalid connection limit: -10
+CREATE DATABASE db_db18 TEMPLATE invalid_template;
+ERROR:  template database "invalid_template" does not exist
+CREATE DATABASE regression; -- DATABASE already exists
+ERROR:  database "regression" already exists
+-- Should fail. CREATE DATABASE with redundant options
+CREATE DATABASE db_db18 TABLESPACE tablespace_db18a TABLESPACE tablespace_db18b;
+ERROR:  conflicting or redundant options
+CREATE DATABASE db_db18 OWNER regress_rol_db18a OWNER regress_rol_db18b;
+ERROR:  conflicting or redundant options
+CREATE DATABASE db_db18 TEMPLATE template_db18a TEMPLATE template_db18b;
+ERROR:  conflicting or redundant options
+CREATE DATABASE db_db18 ENCODING 'UTF8' ENCODING 'UTF8';
+ERROR:  conflicting or redundant options
+CREATE DATABASE db_db18 CONNECTION LIMIT 10 CONNECTION LIMIT 10;
+ERROR:  conflicting or redundant options
+-- Should fail. CREATE DATABASE using non-template DB as template without SUPERUSER
+CREATE ROLE regress_rol_db19 CREATEDB;
+CREATE DATABASE db_db19;
+SET ROLE regress_rol_db19;
+CREATE DATABASE db_db20 TEMPLATE db_db19;
+ERROR:  permission denied to copy database "db_db19"
+RESET ROLE;
+DROP DATABASE db_db19;
+DROP ROLE regress_rol_db19;
+-- Should fail. Trying to CREATE / ALTER / DROP DATABASE without rights
+CREATE ROLE regress_rol_db21;
+CREATE DATABASE db_db21;
+SET ROLE regress_rol_db21;
+ALTER DATABASE regression SET TABLESPACE tablespace_db16;
+ERROR:  must be owner of database regression
+CREATE DATABASE db_db21a;
+ERROR:  permission denied to create database
+ALTER DATABASE db_db21 OWNER TO regress_rol_db21;
+ERROR:  must be owner of database db_db21
+DROP DATABASE db_db21;
+ERROR:  must be owner of database db_db21
+RESET ROLE;
+DROP ROLE regress_rol_db21;
+DROP DATABASE db_db21;
+-- Should fail. CREATE / ALTER DATABASE should not have pg_global as TABLESPACE
+CREATE DATABASE db_db22 TABLESPACE pg_global;
+ERROR:  pg_global cannot be used as default tablespace
+CREATE DATABASE db_db22a;
+ALTER DATABASE db_db22a SET TABLESPACE pg_global;
+ERROR:  pg_global cannot be used as default tablespace
+DROP DATABASE db_db22a;
+-- Should work. ALTER DATABASE SET with bogus OWNER
+BEGIN TRANSACTION;
+ALTER DATABASE regression OWNER TO regress_rol_db23_does_not_exist;
+ERROR:  role "regress_rol_db23_does_not_exist" does not exist
+ROLLBACK;
+-- Should work. ALTER DATABASE SET OWNER to self!
+BEGIN TRANSACTION;
+CREATE ROLE regress_rol_db24;
+ALTER DATABASE regression OWNER TO regress_rol_db24;
+ALTER DATABASE regression OWNER TO regress_rol_db24; -- Set OWNER TO previous OWNER
+ROLLBACK;
diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule
index 3e6b306..be426fa 100644
--- a/src/test/regress/parallel_schedule
+++ b/src/test/regress/parallel_schedule
@@ -88,7 +88,7 @@ test: privileges security_label collate matview
 # ----------
 # Another group of parallel tests
 # ----------
-test: alter_generic misc psql async
+test: alter_generic misc psql async database
 
 # rules cannot run concurrently with any test that creates a view
 test: rules
diff --git a/src/test/regress/serial_schedule b/src/test/regress/serial_schedule
index 3ad289f..6c15500 100644
--- a/src/test/regress/serial_schedule
+++ b/src/test/regress/serial_schedule
@@ -139,3 +139,4 @@ test: largeobject
 test: with
 test: xml
 test: stats
+test: database
diff --git a/src/test/regress/sql/database.sql b/src/test/regress/sql/database.sql
new file mode 100644
index 0000000..4266dbf
--- /dev/null
+++ b/src/test/regress/sql/database.sql
@@ -0,0 +1,120 @@
+--
+-- tests functions in dbcommands.c script
+--
+
+-- Should fail. Check for cases when DROP DATABASE is not allowed
+DROP DATABASE db_db2;   -- doesn't exist
+DROP DATABASE IF EXISTS db_db2; -- doesn't exist with IF EXISTS;
+DROP DATABASE template1;    -- can't drop a template database
+DROP DATABASE regression;   -- can't drop a database in use
+
+-- Should fail. Can't CREATE DATABASE inside a transaction
+BEGIN TRANSACTION;
+CREATE DATABASE db_db3;
+ROLLBACK;
+
+-- Should fail. Can't DROP DATABASE inside a transaction
+BEGIN TRANSACTION;
+DROP DATABASE regression;
+ROLLBACK;
+
+-- Should fail. ALTER DATABASE RENAME TO on invalid DB
+ALTER DATABASE db_db1_nonexistent RENAME TO db_db1a;   -- Database does not exist
+ALTER DATABASE regression RENAME TO regression; -- Database can't be renamed to itself
+--ALTER DATABASE regression RENAME TO db_db1a;   -- Can't rename current database
+
+-- Should fail. ALTER DATABASE RENAME TO without permission
+CREATE ROLE regress_rol_db7;
+SET ROLE regress_rol_db7;
+ALTER DATABASE regression RENAME TO db_db7;
+RESET ROLE;
+DROP ROLE regress_rol_db7;
+
+-- Should fail. ALTER DATABASE RENAME TO with OWNER rights without CREATEDB
+BEGIN TRANSACTION;
+CREATE ROLE regress_rol_db8;
+ALTER DATABASE regression OWNER TO regress_rol_db8;
+SET ROLE regress_rol_db8;
+ALTER DATABASE regression RENAME TO db_db8a;
+ROLLBACK;
+
+-- Should fail. ALTER DATABASE SET / WITH with invalid values
+ALTER DATABASE regression SET invalid_parameter;
+ALTER DATABASE regression WITH CONNECTION LIMIT = -100;
+ALTER DATABASE regression WITH CONNECTION LIMIT = 8ASDF;
+
+-- Should fail. ALTER DATABASE with redundant options
+ALTER DATABASE regression WITH CONNECTION LIMIT 1 CONNECTION LIMIT 2;
+
+-- Should work. ALTER DATABASE SET / WITH with valid values
+ALTER DATABASE regression WITH CONNECTION LIMIT =  -1;
+ALTER DATABASE regression WITH CONNECTION LIMIT =  0;
+ALTER DATABASE regression WITH CONNECTION LIMIT =  100;
+ALTER DATABASE regression SET SEED = 0.5;
+
+-- Should fail. Can't set Tablespace within transaction
+BEGIN TRANSACTION;
+ALTER DATABASE regression SET TABLESPACE pg_default;
+ROLLBACK;
+
+-- Should work. ALTER DATABASE OWNER TO requires CREATEDB rights for grantor
+BEGIN TRANSACTION;
+CREATE ROLE regress_rol_db15;
+CREATE ROLE regress_rol_db14 IN ROLE regress_rol_db15;  -- CREATE regress_rol_db14 to be a member of regress_rol_db15
+ALTER DATABASE regression OWNER TO regress_rol_db14;
+SET ROLE regress_rol_db14;
+ALTER DATABASE regression OWNER TO regress_rol_db15;
+ROLLBACK;
+
+-- Should fail. CREATE DATABASE with invalid values
+CREATE DATABASE db_db18 ENCODING 'invalid_encoding';
+CREATE DATABASE db_db18 ENCODING 123456789;
+CREATE DATABASE db_db18 CONNECTION LIMIT -10;
+CREATE DATABASE db_db18 TEMPLATE invalid_template;
+CREATE DATABASE regression; -- DATABASE already exists
+
+-- Should fail. CREATE DATABASE with redundant options
+CREATE DATABASE db_db18 TABLESPACE tablespace_db18a TABLESPACE tablespace_db18b;
+CREATE DATABASE db_db18 OWNER regress_rol_db18a OWNER regress_rol_db18b;
+CREATE DATABASE db_db18 TEMPLATE template_db18a TEMPLATE template_db18b;
+CREATE DATABASE db_db18 ENCODING 'UTF8' ENCODING 'UTF8';
+CREATE DATABASE db_db18 CONNECTION LIMIT 10 CONNECTION LIMIT 10;
+
+-- Should fail. CREATE DATABASE using non-template DB as template without SUPERUSER
+CREATE ROLE regress_rol_db19 CREATEDB;
+CREATE DATABASE db_db19;
+SET ROLE regress_rol_db19;
+CREATE DATABASE db_db20 TEMPLATE db_db19;
+RESET ROLE;
+DROP DATABASE db_db19;
+DROP ROLE regress_rol_db19;
+
+-- Should fail. Trying to CREATE / ALTER / DROP DATABASE without rights
+CREATE ROLE regress_rol_db21;
+CREATE DATABASE db_db21;
+SET ROLE regress_rol_db21;
+ALTER DATABASE regression SET TABLESPACE tablespace_db16;
+CREATE DATABASE db_db21a;
+ALTER DATABASE db_db21 OWNER TO regress_rol_db21;
+DROP DATABASE db_db21;
+RESET ROLE;
+DROP ROLE regress_rol_db21;
+DROP DATABASE db_db21;
+
+-- Should fail. CREATE / ALTER DATABASE should not have pg_global as TABLESPACE
+CREATE DATABASE db_db22 TABLESPACE pg_global;
+CREATE DATABASE db_db22a;
+ALTER DATABASE db_db22a SET TABLESPACE pg_global;
+DROP DATABASE db_db22a;
+
+-- Should work. ALTER DATABASE SET with bogus OWNER
+BEGIN TRANSACTION;
+ALTER DATABASE regression OWNER TO regress_rol_db23_does_not_exist;
+ROLLBACK;
+
+-- Should work. ALTER DATABASE SET OWNER to self!
+BEGIN TRANSACTION;
+CREATE ROLE regress_rol_db24;
+ALTER DATABASE regression OWNER TO regress_rol_db24;
+ALTER DATABASE regression OWNER TO regress_rol_db24; -- Set OWNER TO previous OWNER
+ROLLBACK;