5

Im trying to create a SQL file to import my database/schema/tables with PHP.

If I use the PgAdmin, it inserts a meta-command \connect after the CREATE TABLE and this generates an error if I run the SQL with pg_query in PHP.

After read the following questions: PostgreSQL: Create schema in specific database and Postgres Creating Schema in a specific database

I ask: How I can create a new schema, inside specific database, with SQL commands (NOT psql commands)? The specific database is not the default named 'postgres' database, but another created by me.

1 Answer 1

4

The short answer is if you are connected to another database you can't with pure SQL.

Due to the fact that there is no use database; in Postgres like in MySQL you have to open a connection to the specific database you want to create the schema in (e.g. with pg_connect in PHP).

The schema itself can be created with the CREATE SCHEMA... sql command as you might know.

Perhaps working with pg_dump and pg_restore is an option for you. I would also have a look at the sources of phpPgAdmin.

I hope that helped a bit.

*Jost

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer! Ill try to do this by another way. Maybe, creating the table first, and after connecting with pg_connect to then run the script to create the schema and tables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.