Question

OK... looked through all of the Q&A's on PostgreSQL and PDO, but no luck answering my question... same problem finding the answer through Google.

Thus I lay my query at the feet of the masters.

I am just getting started with PostgreSQL, coming from a MySQL background. I am using PDO to connect to it, and pgAdminIII to manage the database itself.

How do I connect to a table which has been stuck in a defined tablespace?

$db = new PDO("pgsql:host=127.0.0.1;user=pageserver;password=myPassword;dbname=pageserver;");

is resulting in:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[08006] [7] FATAL: database "pageserver" does not exist' in ~stacktrace~

Before anyone mentions, sure, I should be using try/catch, and I will as this gets further along... but for now the error is helpful.

This database does exist, but all of the databases that i am working with on this project are placed in a tablespace to keep the databases within specific subfolders.

I am assuming this is where my problem is coming in; so far, I have seen no references on tablespaces anywhere through the PDO documentation.

The documentation for PostgreSQL obviously goes into great length on tablespaces, but doesn't reference PDO syntax for connecting to them.

I am hoping it is something simple like:

$db = new PDO("pgsql:host=127.0.0.1;tablespace=system;user=pageserver;password=MyPassword;dbname=pageserver;");

But this results in:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[08006] [7] invalid connection option "tablespace"' in ~stacktrace~

@Igor

Well, I just spent about half an hour running further tests, and exploring the structure, and ensuring that the roles, schemas, tablespaces, and tables were all in line...

They were all in order, and actually set up correctly.

I tried using schema.database name as you recommended, but it didn't work.

Then I discovered the problem that had been plaguing me this entire time.

Apparently, after having setting up the databases, then close to 40 tables, including all of the indexes... my problem wasn't even PostgreSQL.

It was an evil combination of PgAdminIII and my complete lack of sleep.

Apparently, all of these tables got set up under the db 'postgres' instead of under the db's they were meant to be placed within.

My apologies for my idiocy, and thank you for seeing it clearly.

Marked as correct.

Was it helpful?

Solution

First of all: How do I connect to a table ... - you do not connect to a table. You connect to a database. When you are connected to a DB you can acces the table.

Second - tablespaces do not affect in any way the acces to the tables of DB. Tablespaces are only for organising physical storage of the tables. You may have mistaken tablespaces for schems. Schemas are used for logical organisatin of tables inside a DB.

So, to get data from a table in postgres you need:

Connect to the database, that has that table. If you cannot connect to the database, check if it exists, you are connecting to the right server and server is using the right database cluster.

When connected to DB you can select data from any table of this DB. If you need to select data from a table in particul schema of the DB - use the full name of the table -'schema_name.table_name'.

If you do not want to specify a full name of the table every time - you can modify search_path variable to set default schema.

For the second question - for bulk data loading you can use csv import via COPY command. For test data generation - there are pleanty of tools, both paid and open source, for that. You just need to search (or pay) for them.

The general structure of postgre databese.

First - there is a database cluster. It is a folder, that holds the config files, logs, default tablespace and main information about databases in this cluster.

When you start a postgres server you must specify one database cluster it will use.

Next there are databases. When you connect to a postgres server you must specify a database, that exists in the database cluster, the server is using.

Next there are schemas in the database. The default public schema and any number of user created schemas. Schemas are only used to logicaly divide your tables into some subsets.

Next there are tables. They belong to a particular schema of a particular DB. Tables store data in a tablespace.

A database can use any nubre of tablespaces. And a tablespace can be used by any number of databases. Tablespaces only define a folder, where the data of a table (that uses this tablespace) is store. Changes to the tablespaces do not affect the way you connect to a database or the way you select data from tables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top