1

I have to execute a script from Java code which should create a database and then a table in that database. How do I do it in PostgreSQL?

  1. CREATE DATABASE mydb
  2. USE mydb // doesn't work for PostgreSQL
  3. CREATE TABLE mytable (.....)

What SQL do we give for Step 2 in PosgreSQL?

Version : 9.5

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
deepankardixit90
  • 351
  • 4
  • 19

1 Answers1

0

Almost this question has been asked before (Difference Between Schema / Database in MySQL)

This has a reference to chapter 5.8 'Schemas': https://www.postgresql.org/docs/current/ddl-schemas.html

If your answer was 'how are schemas implemented under PostgreSQL', than this might be the correct answer. I think you cannot use use .., and you have to use mydb.mytable. (or set a search pattern SET search_path TO mydb,public;)

Luuk
  • 12,245
  • 5
  • 22
  • 33
  • 2
    Your first link is about the difference between schema and database in MySQL, it is not 'almost this question'. – Mark Rotteveel Sep 14 '19 at 11:02
  • @MarkRotteveel: The question how to use a schema, and raghav know how to do it in MySQL. For the answer of this question it might be smart to know the differences, which is explained in this first link. – Luuk Sep 14 '19 at 16:04