Create tables in a specific schema using psql? - postgresql

I have a batch that allows me to create a database psql and create tables later.
psql -f "%cd%\db.sql" postgres
echo database creted !
pause
psql -f "%cd%\db_table.sql" mabase postgres
echo tables creted !
the db.sql file:
CREATE DATABASE mabase;
and here is the db_table.sql file:
create table myTable(
idTable INT4 not null,
nom VARCHAR(254) null,
date DATE null,
constraint PK_idTable primary key (idTable)
);
This batch runs as a charme and creates the database as well as the table.
My problem is that the table is created in the public default schema. What I would like to do is create a schema first, that I can do it with one more line in my batch:
psql -f "%cd%\schema.sql" mabase postgres
schema.sql file:
CREATE SCHEMA mabase_schema;
Now the problem is to create the table in the new schema and not in the public schema.
I tried with the line:
psql -f "%cd%\db_table.sql" mabase_schema.mabase postgres
But without success !
How will I be able to define the schema in my batch file?

Related

"PostGIS Error: type “geography” does not exist" when using psql

I'm creating a .sql file to create a new database. A couple of my tables are utilizing the Postgis extension. When I attempt to create the database from the commandline:
psql location < location.sql
it fails on the tables that contain columns that are geography datatypes. When I psql into the database:
psql location
and copy paste the create table code from my .sql file, it creates the table fine. I feel like I'm missing a small but important step here
I've been playing around with my .sql file but have had no luck getting around this error
Here's the script I'm using to create the database:
dropdb location
createdb location
psql -d location -c "CREATE EXTENSION postgis;"
psql -d location -c "CREATE EXTENSION postgis_topology;"
psql location < location.sql
and here's the snippet from the location.sql file for the table that it's failing on:
create table public.fips (
fips_id bigserial,
state_code char(2),
county_code char(3),
county_subdivision_code char(5),
place_code char(5),
consolidated_city_code char(5),
longitude double precision,
latitude double precision,
geo_point geography,
area_name text,
PRIMARY KEY (fips_id)
);
geo_point is the one that it's complaining about.
Turns out I needed to prefix the geography datatype with public.

NextVal of postgresql usage

CREATE SEQUENCE :schema.empseq;
CREATE TABLE emp(empid bigint NOT NULL DEFAULT NEXTVAL(':schema.empseq'));
I am execute like psql -d dbname -U username -f emp.sql -v schema=post
Getting an error
schema ":schema" does not exist
The documentation here talks about how psql interpolates values into SQL.
CREATE SEQUENCE :schema.empseq;
CREATE TABLE emp(empid bigint NOT NULL DEFAULT NEXTVAL(:'schema' || '.empseq'));
might work for you.

Alter postgres table primary key UUID to Character Varying

I want to convert my PostgreSQL table primary key UUID to character varying
ALTER TABLE payment_authorization ALTER COLUMN id TYPE VARCHAR;
When I run the above command showing below error, Beacause foreign key constraints failed. In my system have 200 tables. Is there any easy way to change all tables primary key?
Changing all the tables in place will probably be slow and cumbersome.
The easiest solution might be:
export the database with
pg_dump -F p -f dumpfile.sql dbname
replace uuid with text in the dump using an editor:
sed --in-place -e 's/uuid/text/g' dumpfile.sql
drop and re-create the database:
DROP DATABASE dbname;
CREATE DATABASE dbname;
import the dump:
psql -U postgres -d dbname -1 -f dumpfile.sql

Load sql file data in to single table in postgres

I have sql file(single_table_data.sql) that contains data of only one table(I have taken dump of only one table from server)
Now i have to insert this sql file in to only single table in my database,
so how to import sql file in to single table in postgres ?
Edit
For example i had database name SpeedData and table name CurrentTable, so now i want to
insert entire sql file data in to this table CurrentTable
Note: The sql file contains only insert statements(not even create statements)
From the documentation:
psql dbname < infile
This should create a new table with the name of the previously dumped one and insert all data into it. Replace dbname with the name of the new database and infile with the name/path of the file containing the dump, in your case (single_table_data.sql)
If the dump contains only the insert statements, create the table by hand and then execute
psql -U your_username -d dbname -f single_table_data.sql
You can simply import sql dump data into your postgres db.
if you have already created DB then don't need to follow 1st step:-
STEP=1
open terminal then run following commands to create postgres database and user:-
sudo -u postgres psql
postgres=# create database mydb;
postgres=# create user myuser with encrypted password 'mypass';
postgres=# grant all privileges on database mydb to myuser;
STEP=2
\c used for selecting your database.
postgres=# \c yourdatabasename
\i used for importing dump data in database.
yourdatabasename=# \i path_of_your_dump_file for example:-
yourdatabasename=# \i /home/developer/projects/django_projects/db_dump.sql
If you face this type of error when you importing data:-
ERROR: role "yourusername" does not exist
so you can make superuser to your "db_user/yourusername" using this command:-
postgres=# ALTER USER fusion WITH SUPERUSER;
ALTER ROLE
If I understood correctly you want to create table from file and fill in with data.
Correct command is
PGPASSWORD=<password> psql -f /home/.../filename.sql -h localhost -d database_name -U user_name

Dot (.) in Schema name makes pg_dump unusable

I have a schema named 2sample.sc. When I want to pg_dump some of its table, the following error appears:
pg_dump: No matching tables were found
My pg_dump command:
pg_dump -U postgres -t 2sample.sc."error_log" --inserts games > dump.sql
My pg_dump works fine on other schemas like 2sample.
What I did:
I tried to escape dot(.) with no success though
Use "schema.name.with.dots.in.it"."table.name.with.dots.in.it" to specify the schema.table:
-- test schema with a dot in its name
DROP SCHEMA "tmp.tmp" CASCADE;
CREATE SCHEMA "tmp.tmp" ;
SET search_path="tmp.tmp";
CREATE TABLE nononono
( dont SERIAL NOT NULL
);
insert into nononono
SELECT generate_series(1,10)
;
$pg_dump -t \"tmp.tmp\".\"nononono\" --schema-only -U postgres the_database
Output (snipped):
SET search_path = "tmp.tmp", pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: nononono; Type: TABLE; Schema: tmp.tmp; Owner: postgres; Tablespace:
--
CREATE TABLE nononono (
dont integer NOT NULL
);
BTW: why would you want to add a dot to a schema (or table) name? It is asking for trouble. The same for MixedCaseNames. Underscores work just fine.