HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

Create a database and make it "current"

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
createmakedatabasecurrentand

Problem

I have an sql-script that recreates my unit-testing database. At the moment it's run with \i . It goes like

// create user, give him some priveleges

CREATE DATABASE my_database;

CREATE TABLE my_table (
  id UUID primary key,
  data json
);


But my_table is created in postgres database, not in my_database.

How can I specify that my_table should be created in my_database?

I use postgresql 10.5.

Solution

In 'psql', you can change the database you are connected to by using the \c metacommand. So it would look like this:

CREATE DATABASE my_database;
\c my_database
CREATE TABLE my_table (
  id UUID primary key,
  data json
);


This works from '\i' included files. When the \i file is done, you will be left in the new database unless you do something to change back to the old one.

Code Snippets

CREATE DATABASE my_database;
\c my_database
CREATE TABLE my_table (
  id UUID primary key,
  data json
);

Context

StackExchange Database Administrators Q#221194, answer score: 5

Revisions (0)

No revisions yet.