How to add data file to PostgreSQL database tablespace? - postgresql

I am using PostgreSQL and I want to add a new data files to existing tablespace ( like in oracle )
I could not found any description on this on PostgreSQL documentation.
How to do this ?

PotgreSQL works different than Oracle. It has a much simpler concept for data storage. Data blocks, extents and segments don't exist. In the same spirit, a tablespace is not split into multiple data files. You just create the tablespace and PostgreSQL creates the necessary files to store the data.
When creating a tablespace, you can provide a location, where PostgreSQL should store the data:
CREATE TABLESPACE dbspace LOCATION '/data/dbs';
This works similar to bigfile tablespaces in Oracle, where you also don't have to manage data files.

Related

How to know the data files state?

I tried to look into data dictionary of postgresql for long time
but i didn't help.
I'm trying to know what state is the data file in, is it available for the db instance or not
photo with example
I tryed to look into pg_class table
Postgres does not have the concept of "online" (or "offline") data files.
When you create a table it creates a file for that table. For every GB of data, Postgres will add a new file. If you drop the table all files belonging to that table are removed.
So the files for a table are always "online".
For more details on how Postgres stores the data, I recommend reading the chapter Database Physical Storage in the manual.
Or read The Internals of PostgreSQL - Chapter 1: Database Cluster, Databases, and Tables

How to open an existing PostgreSQL database on a different computer?

I have installed PostgreSQL 13.1 on two computers, a laptop and desktop.
I created database using a Tablespace on a portable SSD drive under F:\MyProject\DB\PG_13_202007201\20350. So I expect all the database files to be in there somewhere.
I want to work on the database on the SSD drive which is fine on the laptop that created the database, but when I go to my desktop I can't figure out how to attach (SQL Server style) the existing database/tablespace and continue working on it.
Is there a way to work like this with Postgresql?
You cannot do that.
A tablespace contains just the data files, but essential information is stored in the metadata (the table name, the columns and their data type, constraints, ...) and elsewhere (for example, the commit log determines which table entries you can see and which ones you cannot).
In short, a tablespace is not self-contained, and you cannot use a tablespace from one database cluster with another database cluster.
If you want to move data between databases, use pg_dump.

Is there a way to link (not import!) a dbf table into a PostgreSQL database?

I need to link an external dbf table into an existing PostgreSQL database. I managed to import that table into PostgreSQL but that creates a copy and therefor redundant data. The dbf table is the attribute table of a shapefile, but I don’t need the spatial aspect of it.
The exercise is part of a project to move data from an MS Access database to PostgreSQL hoping that the data then become accessible from QGIS. Th dbf table is at the moment linked into the MS Access database and used in queries (views) which I want to re-build in PostgreSQL.
I found lots of posts about importing dbf tables into but nothing which would work about linking a dbf table. The closest I got was the Foreign Data Wrapper, but I didn’t manage to use it for my purpose. I’m using PostgreSQL with pgAdmin 4.24.
Many thanks
The exercise is part of a project to move data from an MS Access database to PostgreSQL hoping that the data then become accessible from QGIS.
If you must use PostgreSQL in order to provide access to your spatial data from QGIS, I see no other option than importing the shapefile into PostgreSQL (PostGIS). If for whatever reason you do not need the geometries, you can drop the geometry column after importing the shapefile into the database:
ALTER TABLE table_name DROP COLUMN column_name;
Alternative scenario:
If we're talking about static shapefiles and you don't really need to use PostgreSQL, you can use GeoServer to publish this shapefile via Web Feature Service (WFS) - it is at least what I do in small projects. The easiest option would be to copy the shapefiles into a so called GeoServer Data Directory and publish them afterwards. After that you'd be able to access the data from QGIS using its WFS Client.

Postgres SQL Query about database and table space

What is the hierarchy of Database related objects in postgres SQL?
Should it be like, table space must be created at instance level unlike other RDBMS(where we have table space under database).
If so we create the table space at instance level, what is the purpose of database? and what is difference between table space and database on postgres server?
An instance (in PostgreSQL called cluster) is a data directory initialized with initdb with a PostgreSQL server process.
A tablespace is a directory outside the data directory where objects can also be stored. Tablespaces are useful for certain corner cases like distributing I/O or limiting space for a subset of the data.
A database is a container for objects with permissions, organized in schemas.
The difference is that tablespaces are a physical concept, it defines a space where the data are stored, while databases are a logical concept about how data are organized, what they mean, how they are related, who is allowed to access them and so on.
The two concepts are orthogonal.
A database can have tables in several tablespaces, and a tablespace can contain data from several databases.
Database is where you organize all your objects. Tablespace is just storage space for those object.
You can storage your db object in different Tablespace. For example one table is storage in a Tablespace in diskA but another Table use a Tablespace in diskB to improve the performance. Or maybe you need a tablespace for big tables and dont mind use a slow big HDD for those objects.

PostgreSQL dump Temp table

I created a temp table in my PostgreSQL DB using the following query
SELECT * INTO TEMP TABLE tempdata FROM data WHERE id=2004;
Now I want to create a backup of this temp table tempdata.
So i use the following command line execution
"C:\Program Files\PostgreSQL\9.0\bin\pg_dump.exe" -F t -a -U my_admin -t tempdata myDB >"e:\mydump.backup"
I get a message saying
pg_dump: No matching tables were found
Is it possible to create a dump of temp tables?
Am I doing it correctly?
P.S. : I would also want to restore the same.I don't want to use any extra components.
TIA.
I don't think you'll be able to use pg_dump for that temporary table. The problem is that temporary tables only exist within the session where they were created:
PostgreSQL instead requires each session to issue its own CREATE TEMPORARY TABLE command for each temporary table to be used. This allows different sessions to use the same temporary table name for different purposes, whereas the standard's approach constrains all instances of a given temporary table name to have the same table structure.
So you'd create the temporary table in one session but pg_dump would be using a different session that doesn't have your temporary table.
However, COPY should work:
COPY moves data between PostgreSQL tables and standard file-system files.
but you'll either be copying the data to the standard output or a file on the database server (which requires superuser access):
COPY with a file name instructs the PostgreSQL server to directly read from or write to a file. The file must be accessible to the server and the name must be specified from the viewpoint of the server.
[...]
COPY naming a file is only allowed to database superusers, since it allows reading or writing any file that the server has privileges to access.
So using COPY to dump the temporary table straight to a file might not be an option. You can COPY to the standard output though but how well that will work depends on how you're accessing the database.
You might have better luck if you didn't use temporary tables. You would, of course, have to manage unique table names to avoid conflicts with other sessions and you'd have to take care to ensure that your non-temporary temporary tables were dropped when you were done with them.