Create tablespace in postgresql and set is to it is the default for all newly created databases - postgresql

I have created a tablespace named hdd_tablespace and I wan't all new databases to be automatically created there. Basically when I execute:
CREATE DATABASE FOO;
I want this database to be created in hdd_tablespace.
I have updated postgresql.conf with:
default_tablespace = 'hdd_tablespace'
However, new databases are still created in pg_default.

Actually it turned out I had also to update template1 database that is a template database.
I had to;
ALTER DATABASE template1 SET TABLESPACE hdd_tablespace;

Related

Accidentally restored a another database to the 'postgres' database how to correct

I accidentally restored another database as the default postgres database, how do I correct this?
postgres v10 on ubuntu.
To be specific. Log into a database other then postgres.
Then:
DROP DATABASE postgres;
CREATE DATABASE postgres; --You don't need to specify template1 it is the default.
Then do your restore. If you are using pg_restore make sure you use -C to have the database CREATEd properly.

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.

AWS RDS PostgreSQL Tablespace

I created an AWS RDS postgres instance. When creating i was prompted to create user. There is no reason for creating tablespaces with location in AWS RDS? The reason is when creating a database i want the database to goto the tablespace i created. When i run the below command
CREATE USER test;
CREATE TABLESPACE test OWNER test LOCATION '/test_data';
CREATE DATABASE test WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C' LC_CTYPE = 'C' TABLESPACE = test;
ERROR: permission denied for tablespace test
I logged into postgres using test_admin user to run the above commands. The test_admin is the account i created during the RDS instance provisioning. Is that the only account that i should be using above to create all of the DB objects from that point?
Do you want the owner of the database to be test as well? If so, add that into your CREATE DATABASE.... Otherwise, you'll probably need to give the test_admin user permission on the test tablespace before trying to create it.
Tablespaces in postgres RDS are located in /rdsdbdata/db/base/tablespace/
and I think this question has been answered here
Create tablespace on PostgreSQL RDS

PostgreSQL pg_dumpall Loses Schemas

I have a postgres database and I'm trying to use pg_dumpall to create the full script to reproduce my database (also using github to keep track of versions of the dump file).
My question is, why does pg_dumpall lose my database schemas? For instance, I have a schema called "go1". My pg_dumpall script should produce a file with "go1.users" for instance for my users table in schema "go1". But the dump file only produces "users". It loses the schema name.
Is there a way for pg_dumpall to preserve the schemas in the dump file?
It does not lose it. View the generated file - you will find search_path before creating schema objects, like:
...
SET search_path = instruction, pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: file; Type: TABLE; Schema: instruction; Owner: postgres
--
CREATE TABLE file (
details jsonb,
...
would mean it will create instruction.file table

Store postgres database at some other location other than data folder

Is there any way of storing database other than the fixed data directory in postgres? I have a situation where I need to store database at any location irrespective of data directory.
You can add a tablespace.
A tablespace is basically a location to store databases and/or tables. You create a tablespace using CREATE TABLESPACE:
CREATE TABLESPACE mytablespace LOCATION '/path/to/some/location';
You can then create tables directly in that tablespace:
CREATE TABLE whatever (thing integer) TABLESPACE mytablespace;
Or set the default tablespace:
SET default_tablespace = mytablespace;
You can also set the default tablespace at database creation time:
CREATE DATABASE mydatabase TABLESPACE mytablespace;
You are looking for the 'Data Directory' in linux for example its in
/usr/local/pgsql/data
if you install from source code. Each distribution is different though, try reading up on
File Locations in postgres and Creating a Database Cluster
also try using the query
show data_directory;
in windows try looking around
C:\Program Files (x86)\PostgreSQL\[VERSION]\data\global