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.
Related
I have created a database cluster in PostgreSQL-9.6 using pg_createcluster command with user (-u) specified different from default postgres. I would like to know how to add a new/existing database into this cluster instead of the default 'main' cluster?
Conect to the template1 or postgres database in the new cluster and execute sql
CREATE DATABASE etc
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.
It is known that one can copy an init.sql file to be executed on the creation of the container with a docker command similar to this one: COPY init.sql /docker-entrypoint-initdb.d/
Let's consider that one uses a very simple create table postgresql statement:
CREATE TABLE films (
code char(5) CONSTRAINT firstkey PRIMARY KEY,
title varchar(40) NOT NULL,
did integer NOT NULL,
date_prod date,
kind varchar(10),
len interval hour to minute
);
The question is where (in which database) is this table created?
But more importantly is how to set the current working database to specify exactly on which database we are working with??
Not only for this statement but also for all subsequent ones, and they are many!
This explains in detail the initialization of the database. How to create User/Database in script for Docker Postgres.
To put it briefly, the name of the database created during the initialization and where your tables get created by default, is given by the environment variable POSTGRES_DB. If the variable is not set the default value postgres is used instead.
The scripts in the docker-entrypoint-initdb.d folder are executed one by one with the following command:
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -f <your-file>
therefore you are connected to the POSTGRES_DB database (have a look at the docker-entrypoint.sh script).
In your script files you can nevertheless connect to a different database using the meta-command
\connect DBNAME
Easy: set an environment variable POSTGRES_DB.
According to the docs:
This optional environment variable can be used to define a different name for the default database that is created when the image is first started. If it is not specified, then the value of POSTGRES_USER will be used.
I am getting the following error when I try to start my Rails app running a Postgres database:
ActiveRecord::NoDatabaseError
FATAL: role "Divergent" does not exist
To fix this I ran CREATE ROLE Divergent from inside the psql console but it only creates a lowercase divergent.
How then do I create a role name that matches the case of the name that the Postgres db expects me to have (i.e Divergent with a 'D')? Why does the Postgres db expect me to have this name and can I change it?
The problem was in config/database.yml. The username in my development database config was commented out and so postgres was defaulting to using my system username as the default role name. I simply uncommented the line and named it what I wanted. And then in the postgres console I created a role that corresponded to that username. That did the trick.
Quote the user name to have it be case sensitive. Note that you'll need to quote it again whenever you use it inside psql (like GRANT ALL ON table TO "Divergent").
You can also use the createuser command outside of psql, which is case sensitive: createuser Divergent.
I search for a while to find this answer but with no luck.
The situation:
I have Postgresql currently running on my production environment. I am preparing to scale my database and move it to a large server instance. I made the mistake of setting up the initial database with the postgres user who has all permissions, and I would like the new database to be controlled by a custom user I have created. ie The current database's owner is postgres, and I want the new database owner to be pooper.
To dump, I am running:
pg_dump -d database_name > database_name.sql
To restore on separate machine, I am running:
psql database_name < database_name.sql
If the user is the same, ie both postgres, then it will work just fine, but when switching users, my app does not load correctly. Is there a secret to the madness. Nothing stood out to me.
My system:
Debian Wheezy
Postgresql 9.1
Postgis Extension
pg_dump with the --no-owner flag (see pg_dump --help)
Create the new db with the new owner CREATE DATABASE foo OWNER pooper;,
Load via psql -U pooper -d database_name -f database_name.sql.