niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Databases » PostgreSQL
setting serial start value
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
Author Message
Greg Philpott
*nix forums beginner


Joined: 21 Jul 2006
Posts: 3

PostPosted: Fri Jul 21, 2006 1:32 am    Post subject: setting serial start value Reply with quote

I have a table with a field called "id" that is a serial field and
pkey. I would like to set the the sequence to start at 10000 and
increase sequentially from there but I can't seem to get that to
work. any suggestions are greatly appreciated.

I am using version 7.4.7
Greg

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq
Back to top
Michael Fuhr
*nix forums Guru


Joined: 02 Mar 2005
Posts: 674

PostPosted: Fri Jul 21, 2006 2:49 am    Post subject: Re: setting serial start value Reply with quote

On Thu, Jul 20, 2006 at 09:32:56PM -0400, Greg Philpott wrote:
Quote:
I have a table with a field called "id" that is a serial field and
pkey. I would like to set the the sequence to start at 10000 and
increase sequentially from there but I can't seem to get that to
work. any suggestions are greatly appreciated.

What have you tried? ALTER SEQUENCE or setval() should work.

http://www.postgresql.org/docs/7.4/interactive/sql-altersequence.html
http://www.postgresql.org/docs/7.4/interactive/functions-sequence.html

--
Michael Fuhr

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org
Back to top
Greg Philpott
*nix forums beginner


Joined: 21 Jul 2006
Posts: 3

PostPosted: Fri Jul 21, 2006 3:35 am    Post subject: Re: setting serial start value Reply with quote

Hi Michael, from terminal in psql I enter
# ALTER SEQUENCE public.users MINVALUE 9999;
But it doesn't work. I don't think I am specifying the field
correctly. the schema is public, the table is users, the field is id.
Thanks,
Greg
On 20-Jul-06, at 10:49 PM, Michael Fuhr wrote:

Quote:
On Thu, Jul 20, 2006 at 09:32:56PM -0400, Greg Philpott wrote:
I have a table with a field called "id" that is a serial field and
pkey. I would like to set the the sequence to start at 10000 and
increase sequentially from there but I can't seem to get that to
work. any suggestions are greatly appreciated.

What have you tried? ALTER SEQUENCE or setval() should work.

http://www.postgresql.org/docs/7.4/interactive/sql-altersequence.html
http://www.postgresql.org/docs/7.4/interactive/functions-sequence.html

--
Michael Fuhr


---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq
Back to top
Michael Fuhr
*nix forums Guru


Joined: 02 Mar 2005
Posts: 674

PostPosted: Fri Jul 21, 2006 4:05 am    Post subject: Re: setting serial start value Reply with quote

On Thu, Jul 20, 2006 at 11:35:51PM -0400, Greg Philpott wrote:
Quote:
Hi Michael, from terminal in psql I enter
# ALTER SEQUENCE public.users MINVALUE 9999;
But it doesn't work. I don't think I am specifying the field
correctly. the schema is public, the table is users, the field is id.

ALTER SEQUENCE uses the sequence name, not the table name. Also,
use RESTART WITH to set the sequence's current value. Example:

test=> CREATE TABLE users (id serial PRIMARY KEY, username text);
NOTICE: CREATE TABLE will create implicit sequence "users_id_seq" for "serial" column "users.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "users_pkey" for table "users"
CREATE TABLE
test=> ALTER SEQUENCE users_id_seq RESTART WITH 10000;
ALTER SEQUENCE
test=> INSERT INTO users (username) VALUES ('Alice');
INSERT 2592322 1
test=> INSERT INTO users (username) VALUES ('Bob');
INSERT 2592323 1
test=> SELECT * FROM users;
id | username
-------+----------
10000 | Alice
10001 | Bob
(2 rows)

--
Michael Fuhr

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq
Back to top
Greg Philpott
*nix forums beginner


Joined: 21 Jul 2006
Posts: 3

PostPosted: Fri Jul 21, 2006 4:26 am    Post subject: Re: setting serial start value Reply with quote

Thanks Michael that did the trick!
Greg
On 21-Jul-06, at 12:05 AM, Michael Fuhr wrote:

Quote:
On Thu, Jul 20, 2006 at 11:35:51PM -0400, Greg Philpott wrote:
Hi Michael, from terminal in psql I enter
# ALTER SEQUENCE public.users MINVALUE 9999;
But it doesn't work. I don't think I am specifying the field
correctly. the schema is public, the table is users, the field is id.

ALTER SEQUENCE uses the sequence name, not the table name. Also,
use RESTART WITH to set the sequence's current value. Example:

test=> CREATE TABLE users (id serial PRIMARY KEY, username text);
NOTICE: CREATE TABLE will create implicit sequence "users_id_seq"
for "serial" column "users.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"users_pkey" for table "users"
CREATE TABLE
test=> ALTER SEQUENCE users_id_seq RESTART WITH 10000;
ALTER SEQUENCE
test=> INSERT INTO users (username) VALUES ('Alice');
INSERT 2592322 1
test=> INSERT INTO users (username) VALUES ('Bob');
INSERT 2592323 1
test=> SELECT * FROM users;
id | username
-------+----------
10000 | Alice
10001 | Bob
(2 rows)

--
Michael Fuhr


---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
The time now is Sun Nov 23, 2008 1:38 pm | All times are GMT
navigation Forum index » Databases » PostgreSQL
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts does squid 2.6 support setting cache_peer port in redirec... Victor Tsang Squid 0 Fri Jul 21, 2006 8:16 am
No new posts help setting up exim -- mail not sent to domain ithou.org Albert Wong Exim 0 Fri Jul 21, 2006 7:00 am
No new posts bind keyboard POWER button to start some program Vladi Lemurov Debian 1 Fri Jul 21, 2006 6:00 am
No new posts setting up listener password in Oracle 10g sl Server 2 Thu Jul 20, 2006 4:48 pm
No new posts Is there a boot loader that can load a file a start up an... christopher.mcrorie@gmail Setup 1 Thu Jul 20, 2006 2:57 am

Mortgage Loans | Cheapest mobile phones | Advertising | Ringtone | Mortgage Calculator
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1848s ][ Queries: 16 (0.0938s) ][ GZIP on - Debug on ]