postgresql: creating database in a specified location - postgresql

I am trying to create a PG db in a specified location. According to the docs here, I must use the LOCATION flag. However, when I run CREATE DATABASE (from the pgsql CLI), I get the following warning:
WARNING: LOCATION is not supported anymore
HINT: Consider using tablespaces instead.
However, the PG documentation on TABLESPACES does not show how it can be used to create a database in a specific directory. What is the required syntax to do this?

You would need to do this in 2 steps:
Create Tablespace examples for which you can see in the link
Create Database
When you create tablespace you set it's location and then you can create multiple databases in the same tablespace if you choose to.

CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';
See the chapter about tablespaces in the manual.

Related

How to DROP tables from specific database-schema in Postgresql?

I am new to Postgresql and so far I have not found a way to drop a table from specific database. To give some context:
We are doing a synchronization from Oracle to PostgreSQL of 5 tables. In postgres I have a database SoloCopy and the schema is the default public. In the Postgresql instance we have also 2 more databases SoloSynch and postgres (the default one).
What I want to do is to select SoloCopy database and:
DROP TABLE public.table1;
When I do the above DROP statement table1 is deleted only from the database that was selected when opening SQL Query. But I want to specify the database before that and to be irrelevant from where the SQL Query was open. How can I do that?
I found an answer on my own. Setup can be found here:
Psql in Task Scheduler does not run a file script
Basically I needed to use PSQL and with the connection string there I connect to a specific DB, which I can drop the tables from. The details for PGPASSWORD and the .bat file I ended up creating are in the link above. Had to start everything from CMD and switch to psql from there.

pg_dump and friends: backup and restore using tablespace

Given a database dump how to specify a tablespace to be used by all tables during restore? The database has multiple tablespaces used by its table. Old tablespaces should be ignored (they are not relevant on new computer) and all tablespaces my by replaced by a new one.
dump with "--no-tablespaces" parameter to have tablespaces-free dump - but you can also use the same parameter on pg_restore if you cannot change dump commands
set global parameter "default_tablespace" on target DB to what is needed for restore (for example by using alter database xxxxx set DEFAULT_TABLESPACE='xxx')
run all pg_restore tasks
if necessary reset default_tablespace to original value

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

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.

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

Postgis installation: type "geometry" does not exist

I am trying to create table with Postgis. I do it by this page. But when I import postgis.sql file, I get a lot of errors:
ERROR: type "geometry" does not exist
Does anybody know how can I fix it?
I had the same problem, but it was fixed by running following code
CREATE EXTENSION postgis;
In detail,
open pgAdmin
select (click) your database
click "SQL" icon on the bar
run "CREATE EXTENSION postgis;" code
If the Postgis-Extension is loaded, then your SQL perhaps does not find the geometry-type because of missing search-path to the public schema.
Try
SET search_path = ..., public;
in the first line of your scsript. (replace ... with the other required search-paths)
You can do it from terminal:
psql mydatabasename -c "CREATE EXTENSION postgis";
To get psql to stop on the first error, use -v ON_ERROR_STOP=1 (which is off by default, which is why you see many errors). For example:
psql -U postgres -d postgis -v ON_ERROR_STOP=1 -f postgis.sql
The actual error is something like "could not load library X", which can vary on your situation. As a guess, try this command before installing the sql script:
ldconfig
(you might need to prefix with sudo depending on your system). This command updates the paths to all system libraries, such as GEOS.
This error may also occur if you try to use postgis types on another schema rather than public.
If you are creating you own schema, using postgis 2.3 or higher and encounter this error, do the following as stated here:
CREATE SCHEMA IF NOT EXISTS my_schema;
CREATE extension postgis;
UPDATE pg_extension
SET extrelocatable = TRUE
WHERE extname = 'postgis';
ALTER EXTENSION postgis
SET SCHEMA my_schema;
ALTER EXTENSION postgis
UPDATE TO "2.5.2next";
ALTER EXTENSION postgis
UPDATE TO "2.5.2";
SET search_path TO my_schema;
Then you can proceed to use postgis functinalities.
You must enable the extension on your database.
psql my_database -c "CREATE EXTENSION postgis;"
You also need to ensure that the user you are trying to use the postgis extension as, has access to the schema where postgis is setup (which in the tutorials I read is called 'postgis').
I just had this error, and it was solved because I had only given a new user access to the database. In the database I'd created, I ran:
grant all on schema postgis to USERNAME;
And this solved this error
run this query first:
"CREATE EXTENSION postgis"
The answers here may solve your problem, however if you already have postgis enabled on your DB, the issue may be that you are trying to restore a postgis table (with a geometry column) into a schema other than where your postgis extension is enabled. In pgAdmin you can click on the postgis extension and see which schema is specified. If you are trying to restore a table with geometry column into a different schema, you might get this error.
I resolved this by altering my postgis extension - however I'm not sure if that was necessarily the best way to do it. All I know is that it allowed me to restore the table.
First make sure you have (matching to pg version: psql -V) postgis installed:
sudo apt install postgis postgresql-9.6-postgis-2.3
Just before tables creation add:
db.engine.execute('create extension postgis')
db.create_all()
This has already been answered but I wanted to add a more thorough answer that explains why certain commands work, and in what circumstances to use them, and of course, how to figure out which circumstances you are in.
First, you need to check that PostGIS is actually installed on your box. When connected to postgres, such as via psql, run:
SELECT PostGIS_Full_Version();
If it's not installed, look up distro- and version-specific instructions for installing PostGIS and install it.
Assuming PostGIS is installed, the error is usually the result of not having "created" (this is an unfortunately misleading use of language, the effect is more like "enabling" the extension) the extension for the particular database. The way PostgreSQL is set up, by default new databases do not come with any extensions enabled, and you need to enable ("create") them per-database. In order to do this you need to run the following command.
It only needs to be run once:
CREATE EXTENSION postgis;
I think you need superuser privileges for the particular database in question, in order to run this command.
Assuming postgres is configured so that the permissions allow, you can execute this command from the command line by running the following command:
psql my_database -c "CREATE EXTENSION postgis;"
You may need to use the -U flag and specify a user.
In some cases, however, the extension may have already been created, and installed under a different schema than public, and the problem may be one of permissions. This can arise like in the situation #mozboz describes, if you create a new user but don't give it access to the schema. To detect this case, look for a separate schema in the database, with a table called spatial_ref_sys, as this is created when the extension is created.
In this case you may need to run, when connected to the database:
GRANT USAGE ON SCHEMA schema_name TO username;
In my experience, this situation is rare, and I have never found any reason to set things up this way. The schema_name is often, but not always postgis. By default if you run the first command here, it will create the extension under the public schema.
I think USAGE is usually sufficient for most cases, but you might want to grant more privileges if you want the user to be able to actually edit data; the only time this has ever come up for me was adding new projections to spatial_ref_system, but even this is rare as by default that table includes most commonly used projections.
Or...
cursor.execute('create extension postgis')
in your python program, using a current cursor from psycopg2.
My experience was I was trying to load the ism file to a different file than "public". I initialised the postgis extension on another schema other than public. So that didn't work. So finally I had to remove the extension, and than created the postgis extension in public. After that I could load the osm into my new schema
Osm always looks for the extension file in public, irregardless of where u intend to install the osm files in another schema
Verify the public search_path is not included for the user:
SELECT usename, useconfig from pg_user;
-- { search_path=pg_catalog, public }
SHOW SEARCH_PATH;
-- public, topology
Method #1: SET the search_path to public
SET search_path = "public";
CREATE TABLE IF NOT EXISTS sample_geom
(
geom_1 GEOMETRY,
geom_2 GEOMETRY(Polygon, 4326) NOT NULL
);
ALTER TABLE sample_geom
OWNER TO root;
Method #2: Use the qualified object name for the GEOMETRY object type (public.GEOMETRY)
CREATE TABLE IF NOT EXISTS sample_geom
(
geom_1 public.GEOMETRY,
geom_2 public.GEOMETRY(Polygon, 4326) NOT NULL
);
ALTER TABLE sample_geom
OWNER TO root;
Source: Demystifying Schemas & search_path through Examples
Using pgAdmin 4,you can sort this:
Click on the SQL query button (or go to Tools >
Query Tool).
Enter the following query into the query text field to load the PostGIS spatial extension and Click the Play button in the toolbar (or press F5) to “Execute the query.”
CREATE EXTENSION postgis;
Succesful feedback
Now confirm that PostGIS is installed by running a PostGIS function:
SELECT postgis_full_version();
Code
Feedback