0

For creating new database, in pgAdmin4 how can write the SQL code to check there is another Database that the name is same to our database or not?

CREATE DATABASE sportner
with
OWNER =  POSTGRES           
ENCODING = 'UTF8'       
TABLESPACE = pg_default
CONNECTION LIMIT = -1;
sargol
  • 55
  • 1
  • 8

2 Answers2

0
  1. understand that a PostgreSQL instance has many databases, and a database has many schemas. This is different from MySQL, for which an instance has many schemas (which are called "databases")

  2. understand that PostgreSQL's databases are segregated from each other - on purpose, and for good reason. You need a foreign data wrapper for cross-database communications

There are a couple ways to list the databases in a PG cluster:

  1. using psql (CLI): use the \list command
  2. using SQL: SELECT datname FROM pg_database WHERE datistemplate = false;

This answer tells you how to create a database conditionally

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
  • Thanks a lot for your help the Error is : CREATE DATABASE cannot run inside a transaction block – sargol Jan 28 '20 at 21:56
0
DROP DATABASE IF EXISTS SPORTNER;
CREATE DATABASE sportner
with
OWNER =  POSTGRES
ENCODING = 'UTF8'       
TABLESPACE = pg_default`enter code here`
CONNECTION LIMIT = -1;
sargol
  • 55
  • 1
  • 8
  • 1
    Code-only answers are considered low quality: make sure to provide an explanation what your code does and how it solves the problem. It will help the asker and future readers both if you can add more information in your post. See also Explaining entirely code-based answers: https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers – borchvm Jan 30 '20 at 07:16