What's the difference between initdb /usr/local/var/[db] and createdb [db] - postgresql

I am starting to use PostgreSQL and I am confused about the two ways to create a database. When I installed it the first time, the instructions said I have to create a default database with initdb /usr/local/var/postgres When I lookup my databases, I can see that I have a database called postgres. Now I am able to create a database with two other commands whereas the former is the command line script and the latter the SQL command. In the case of a "postgres" called database it would be:
createdb postgres
CREATE DATABASE postgres
Both are setting up a database in my list of databases. When I try to create another database with initdb /usr/local/var/[someDbName] though, it doesn't appear in my list of databases. So what's the difference between initdb and createdb then?

initdb is not used to create a "new database".
As documented in the manual you need it to create a "cluster" or "data directory" which then stores databases created with create database.
Quote from the manual:
Before you can do anything, you must initialize a database storage area on disk. We call this a database cluster. (The SQL standard uses the term catalog cluster.) A database cluster is a collection of databases that is managed by a single instance of a running database server
[...]
In file system terms, a database cluster is a single directory under which all data will be stored. We call this the data directory or data area
In short: initdb creates the necessary directory layout on the harddisk to be able to create and manage databases.
It's a necessary part of the installation process of a Postgres server.

Related

How to set default database name when creating a cluster

When creating a cluster using initdb
$ initdb -D /usr/local/pgsql/data
it creates a default database user postgres and database name postgres.
I need to create a cluster with a different superuser name and default database name. I found the way to have a different name for superuser:
$ initdb --username=myuser
But can't find how to define the different name for the database instead of postgres. How can I do that? Is my only option is to rename the default DB after it's been created?
Creating the database "postgres" is hardcoded in the initdb-code. You could build your own version of initdb and use a different name for this specific database or you run a script afterwards that renames this database.

Postgres - required databases

I have accidentally deleted the default "postgres" database from my postgres. I've read that:
Most Postgres servers have three databases defined by default: template0 , template1 and postgres . template0 and template1 are skeleton databases that are or can be used by the CREATE DATABASE command. postgres is the default database you will connect to before you have created any other databases.
I have now created again a postgres database by running CREATE DATABASE postgres.
Do I need to do anything else to basically redo deleting the "postgres" database? Or the current one is basically the same?
Thanks
The database postgres is in no way special. You should use the bootstrap superuser (normally postgres) as the database owner, then the database will be just as good as the original postgres database.
The only difference is that the new database will have an OID ≥ 16384, which identifies it as an object created after cluster initialization. However, a quick look through the source code makes me believe that we don't use that anywhere.

How to shut down one database in a db2 instance?

I want to shut down one database in a db2 instance with multiple dbs.
I don't want to deactivate the db as it will reconnect when I try to connect. It should be completely shut down so I get a connection error when trying to connect to the db.
This is not a programming question so it can be viewed as off topic.
There are different techniques, each has advantages/disadvantages.
You can quiesce the database and later unquiesce it.
or you can revoke connect rights, and later grant them, but this depends on how well your role separation is done.
or you force off existing applications and then connect in exclusive mode as the instance owner (provided that your applications NEVER connect with instance-owner credentials).
One trick you could use is to temporarily recatalog the database you want to deactivate under a different name; this will prevent applications from connecting to it using the original name, regardless of the authority they use.
First, determine the database path by looking at its catalog entry:
db2 list db directory
The value of the "Local database directory" property is what you need.
Now you can recatalog the database:
db2 uncatalog db orig_db
db2 catalog db orig_db as foobar on <path>
where <path> is the local database directory determined previously.
Once you force all applications currently connected to the database in question you will be able to deactivate the database:
db2 list applications
db2 "force application (<app id 1>, <app id 2>,...)
db2 deactivate db foobar
Later on you can restore the catalog entry to its original value:
db2 uncatalog db foobar
db2 catalog db orig_db on <path>

Restore database from production to Development

We have the database named 'itreport' on production server and database named 'itreport_dev' on development server.
1)On Production server, 52 users are present in the database 'itreport'.
2)On Development server, 60 users are in present the database 'itreport_dev'.
3)I have taken the dump of production server database 'itreport'. Dump file name is backup_12082017.sql
My question is
If I restore the above dump(backup) file to Development server database 'itreport_dev, Users(60) present will present in the Development database?
If not what option we have to give in the restore process?
What are the pre steps and post steps to be performed on Develpement server?
Short answer: No, roles are not part of a single-database backup.
If you dump only the database using pg_dump it will only restore tables and data. not any roles. any objects owned by missing roles will end up owned by the user performing the restore (this user should be a superuser)
If you do pg_dumpall roles and all databases will be backed up.
Roles can be backed up separately using pg_dumpall -r
if you do pgdumpall --clean the resore will destroy and replace any databases and roles on the dev server that also exist in the dump. any names that are not in both will be unaffected, (the special role "postgres" and template databases also are untouched)
pgdumpall backups are SQL backups and should be restores using psql
su postgres -c psql < all-database-backupfile.sql
or
zcat all-database-backupfile.sql.gz | su postgres -c psql
etc.
(for windows use runas instead of su, I'm not sure of the exact syntax needed)

Default databases in PostgreSQL

What are the default databases in PostgreSQL at the time of installation?
template1: the "default" database, which is copied when you perform "create database foo"
template0: the "minimal default" database, which serves essentially the same purpose, but typically used to create databases when restoring dumps which might already have the extra objects that are in template1, or to create a database that uses a different character encoding to the server default (template0 should only contain ASCII characters in strings)
postgres: an "administrative" database, which clients can assume exists to connect to merely to list which databases are available etc. Also, for example, pgAdmin will install the pg_agent schema in this database.
Apparently there is a database "postgres" that is created by default on each postgresql server installation.
It appears that it does not really have a well-defined purpose. According to the docs:
Creating a database cluster consists of creating the directories in
which the database data will live, generating the shared catalog
tables (tables that belong to the whole cluster rather than to any
particular database), and creating the template1 and postgres
databases. When you later create a new database, everything in the
template1 database is copied. (Therefore, anything installed in
template1 is automatically copied into each database created later.)
The postgres database is a default database meant for use by users,
utilities and third party applications.
(Source: http://www.postgresql.org/docs/8.2/static/app-initdb.html )